Java Solution - Iterative over each possible solution


  • 0
    M

    static void swap(int[] c, int i, int j) {
    int temp = c[i];
    c[i] = c[j];
    c[j] = temp;
    }

    public static List<List<Integer>> permute(int[] nums) {
    	List<List<Integer>> l = new ArrayList<>();
    	permute(nums, 0, nums.length - 1, l);
    	return l;
    
    }
    
    private static void permute(int[] nums, int low, int high, List<List<Integer>> l) {
    	if (low == high) {
    		List<Integer> arrList = new ArrayList<>();
    		for (int i : nums) {
    			arrList.add(i);
    		}
    		l.add(arrList);
    	}
    	for (int i = low; i <= high; i++) {
    		swap(nums, i, low);
    		permute(nums, low + 1, high, l);
    		swap(nums, i, low);
    	}
    }
    

    }


Log in to reply
 

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