My solution works on Eclipse but not OJ


  • 0
    M
    public static List<List<Integer>> permute(int[] nums) {
     if(nums == null || nums.length == 0) return result;
     // turns into a list
     ArrayList<Integer> list = new ArrayList();
     for(int i: nums) list.add(i);
     permuteHelper(list, 0);
     return result;}
    
    
     public static void permuteHelper(ArrayList<Integer> list, int start){
     if(start >= list.size()){
    	 result.add(new ArrayList(list));
     }
     HashSet<Integer> visited = new HashSet();
     for(int i=start; i<list.size(); i++){
    	 if(visited.add(list.get(i))){
    	 Collections.swap(list,start,i);
    	 permuteHelper(list, start+1);
    	 Collections.swap(list,start,i);
    	 }
     }}

Log in to reply
 

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