http://www.lintcode.com/en/problem/permutations/\#

class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    public List<List<Integer>> permute(int[] nums) {
        // write your code here
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> cur = new ArrayList<Integer>();
        if (nums == null || nums.length == 0) {
            ans.add(cur);
            return ans;
        }


        permute(nums, cur, ans);
        return ans;


    }

    public void permute(int[] nums,
                        List<Integer> cur,
                        List<List<Integer>> ans) {
        if (cur.size() == nums.length) {
            ans.add(new ArrayList<Integer>(cur));
            return;
        }


        for (int i = 0; i < nums.length; i++) {
            if (cur.contains(nums[i])) {
                continue;
            }

            cur.add(nums[i]);
            permute(nums, cur, ans);
            cur.remove(cur.size() - 1);
        }

    }
}

results matching ""

    No results matching ""