Simple Java Solution


  • 0
    Z
    import java.util.ArrayList;
    import java.util.List;
    
    public class Solution {
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            if (nums == null || nums.length == 0) {
                return result;
            }
            perm(result, nums, 0, nums.length - 1);
            return result;
        }
    
        public void perm(List<List<Integer>> result, int[] nums, int start, int end) {
            if (start >= end) {
                List<Integer> list = new ArrayList<>();
                for (int num : nums) {
                    list.add(num);
                }
                result.add(list);
            } else {
                for (int i = start; i <= end; i++) {
                    swap(nums, start, i);
                    perm(result, nums, start + 1, end);
                    swap(nums, start, i);
                }
            }
        }
    
        public 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.