Easy Java AC solution


  • 0
    W
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            perm(0, nums, res);
            return res;
        }
        private void perm(int start, int[] nums, List<List<Integer>> res) {
            if (start == nums.length - 1) {
                res.add(IntStream.of(nums).boxed().collect(Collectors.toList()));
                return;
            }
            
            for (int i = start; i < nums.length; i++) {
                swap(nums, i, start);
                perm(start + 1, nums, res);
                swap(nums, i, start);
            }
        }
        private void swap(int[] nums, int i, int j) {
    	    int tmp = nums[i];
    	    nums[i] = nums[j];
    	    nums[j] = tmp;
        }
    

Log in to reply
 

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