What wrong with this code?


  • 0
    L
    import java.util.ArrayList;
    import java.util.List;
    public class Solution
    {
    private  static  char[]  strs; 	 
    public  static  List<String> generateParenthesis(int n)
    {	
    strs=new char[2*n+1];	
    List<String> result=new ArrayList<String>();
    result.addAll(generateParenthesis(n, 0, 0));
    return result;
    }
    public static List<String> generateParenthesis(int n,int left,int  right)
    {
    List<String> result=new  ArrayList<String>();  	
    if(left==right&&left+right==2*n)
    {
    result.add(new String(strs));	
    return  result ;	
    }
    if(left<right||left+right>=2*n)
    {	
    return new  ArrayList<>(); 
    }
    int index=left+right;
    strs[index]='(';
    result.addAll(generateParenthesis(n, left+1, right));
    strs[index]=')';
    result.addAll(generateParenthesis(n, left, right+1));
    return  result;   
    }             
    }
    

    i have test the code on local machine ,i think is the right answer,but when i submit to the site,
    it give me
    Input: 1

    Output:["()"]

    Expected: ["()"]

    any one could some advise?


  • 0
    L

    i just want wo know why the output same as the Expected ,why it give me wrong answer, the thinking is easy,use recursive to solve this problem ,


Log in to reply
 

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