Java Clear StringBuilder Solution


  • -1
    S

    Though it's not faster than using String.

    public class Solution {
        ArrayList<String> result = new ArrayList<>();
        
        public List<String> generateParenthesis(int n) {
            generate(n, new StringBuilder(), 0);
            return result;
        }
        
        private void generate(int remainLeft, StringBuilder s, int needRight) {
            if (needRight != 0) {
                generate(remainLeft, s.append(')'), needRight - 1);
                s.deleteCharAt(s.length()-1);
            }
            if (remainLeft != 0) {
                generate(remainLeft-1, s.append('('), needRight + 1);
                s.deleteCharAt(s.length()-1);
            }
            if(needRight == 0 && remainLeft == 0) {
                result.add(s.toString());
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.