入栈序列求所有出栈顺序

卡特兰数:

 

    public static void dp(List result, String temp, int index, Stack stack, List origin) {
        if(temp.length() == origin.size()) {
            StringBuilder sb = new StringBuilder();
            for(char c : temp.toCharArray()) {
                sb.append(c + "");
            }
            result.add(sb.toString());
            return;
        }
        int save = 0;
        boolean flag = false;
        if(stack.size() != 0) {
            flag = true;
            save = (int) stack.pop();
            dp(result, temp + save, index, stack, origin);
        }
        if(index < origin.size()) {
            if(flag) {
                stack.push(save);
                flag = !flag;
            }
            stack.push(origin.get(index));
            dp(result, temp, index + 1, stack, origin);
        }
        return;
    }

 

你可能感兴趣的:(Just,code)