Why the "Run Code" and "Submit" give me the different answer with the same input?


  • -1

    the code like this

    class Solution {
    	public static List<String> reu = new ArrayList<>();
    	public static void swap(String[] str, int i, int j) {
    		String temp = new String();
    		temp = str[i];
    		str[i] = str[j];
    		str[j] = temp;
    	}
    	public static void arrange(String[] str, int st, int len) {
    		if(st == len - 1) {
    			
    			String s = new String();
    			for(int i=0; i<len; i++) {
    				s = s + str[i];
    			}
    			if(isValid(s)) {
    				if(!reu.contains(s))
    					reu.add(s);
    			}
    			else s = null;
    		}
    		else {
    			for(int i = st; i<len; i++) {
    				swap(str, st, i);
    				arrange(str, st + 1, len);
    				swap(str, st, i);
    			}
    		}
    	}
    	public static boolean isValid(String s) {
            ArrayDeque<Character> stack = new ArrayDeque<>();
            stack.push('a');
            if(s.length() % 2 == 1) return false;
            for(int i=0; i<s.length(); i++) {
            	if(stack.peek() == '(' && s.charAt(i) == ')') {
            		stack.pop();
            	}
            	else
            		stack.push(s.charAt(i));
            }
            stack.pollFirst();
            if(stack.isEmpty()) return true;
            else return false;
        }
    	 public List<String> generateParenthesis(int n) {
    		 	String[] str = new String[n + n];
    		 	for(int i=0; i<n+n; i += 2) {
    		 		str[i] = "(";
    		 		str[i+1] = ")";
    		 	}
    		 	arrange(str, 0, str.length);
    		 	return reu;
    	    }
    }
    

    with the testcase "1"
    the "Submit" gives me the answer :["()()()","()(())","(())()","(()())","((()))","()"]
    but both the "Run code" and my PC give me the right answer ["()"]?
    3q


Log in to reply
 

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