Generate parenthse java


  • 0
    V

    public class Solution {

    List<String> list = new ArrayList<String>();
    
    public List<String> generateParenthesis(int n) {
        
       if(n == 0){
           return list;
       }
       
       loadKuoHao(list, new StringBuilder(), n, n);
       return list;
        
    }
    
    public void loadKuoHao(List<String> list, StringBuilder sb, int zuoKuoHao, int youKuoHao){
       if(zuoKuoHao < 0 || youKuoHao < 0 || zuoKuoHao > youKuoHao){
           return;
       }
       
       if(zuoKuoHao == 0 && youKuoHao == 0){
           list.add(sb.toString());
           return;
       }
       
       loadKuoHao(list, sb.append("("), zuoKuoHao - 1, youKuoHao);
       sb.deleteCharAt(sb.length() - 1);
       
       loadKuoHao(list, sb.append(")"), zuoKuoHao, youKuoHao - 1);
       sb.deleteCharAt(sb.length() - 1);
    }
    

    }


  • 0
    H

    hahah ni de pin yin ming ming hao niu bi


  • 0
    W

    KuoHao.....ZuoKuoHao,....YouKuoHao.....


Log in to reply
 

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