1ms Java solution using character array.


  • 0
    K

    recursion solution.

        private void parenthesis_r(int n,int countL,int countR,char[] out,List<String> result){
            if(countL<n) {
                out[countL+countR]='(';
                parenthesis_r(n,++countL,countR,out,result);
                countL--;
            }
            if(countL-countR>0) {
                if(countR<n){
                    out[countL+countR]=')';
                    if(++countR==n) {
                        result.add(new String(out));
                        return;
                    }
                    parenthesis_r(n,countL,countR,out,result);
                    countR--;
    
    
                }
            }
        }
    

Log in to reply
 

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