46. Permutations

we use swap swap for this one: 




class
Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); int[] copy = nums; dfs(result, copy, 0); return result; } private void dfs(List<List<Integer>> result, int[] copy, int index){ if(index == copy.length){ // can't access nums from here //result.add(Arrays.asList(copy)); // new ArrayList<>(), vs Arrays.asList<>(). ?? //result.add(new ArrayList<>(copy)); Line 12: error: cannot infer type arguments for ArrayList<> List<Integer> ans = arrayToList(copy); result.add(ans); return; } for(int i = index; i < copy.length; i++){ swap(copy, index , i ); dfs(result, copy, index + 1); // swap back , backtrack recovery swap(copy, index, i); } } private List<Integer> arrayToList(int[] copy){ List<Integer> list = new ArrayList<>(); for(int num : copy){ list.add(num); } return list; } private void swap(int[] copy, int index, int i){ int tmp = copy[index]; copy[index] = copy[i]; copy[i] = tmp; } }

 

 solution 2 : 

(1)

used hashset to record used elements and pass the hashset for the next level 

on the current level, we need to do the backtrack recovery steps for the hashset and the tmp list. 

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> tmp = new ArrayList<>();
        HashSet<Integer> used = new HashSet<>();
        dfs(result, tmp, used, nums, 0);
        return result;
    }
    
    private void dfs(List<List<Integer>> result, List<Integer> tmp, HashSet<Integer> used, int[] nums, int index){
        if(index == nums.length){
            result.add(new ArrayList<>(tmp));
            return;
        }
        
        for(int i = 0; i < nums.length; i++){
            if(!used.contains(nums[i])){  // contains , used is a set,
                tmp.add(nums[i]);
                used.add(nums[i]);
                dfs(result, tmp, used, nums, index + 1);
                // tmp is changed , backtrack recovery
                tmp.remove(tmp.size() - 1); // tmp.remove(nums[i])  run time error . in a list, just remove the last one 
                used.remove(nums[i]);
            }
        }
    }
}

 (2)

used boolean[]  to record used elements and pass the boolean[] for the next level 

on the current level, we need to do the backtrack recovery steps for the boolean[] and the tmp list. 

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> tmp = new ArrayList<>();
        boolean[] used = new boolean[nums.length]; // size, otherwise , array dimension missing
        dfs(result, tmp, used, nums, 0);
        return result;
    }
    
    private void dfs(List<List<Integer>> result, List<Integer> tmp, boolean[] used, int[] nums, int index){
        if(index == nums.length){
            result.add(new ArrayList<>(tmp));
            return;
        }
        
        for(int i = 0; i < nums.length; i++){
            if(!used[i]){  // used is a boolean[], !used[i] is the same as saying used[i] == false
                tmp.add(nums[i]);
                used[i] = true;
                dfs(result, tmp, used, nums, index + 1);
                // tmp is changed , backtrack recovery
                tmp.remove(tmp.size() - 1); // tmp.remove(nums[i])  run time error . in a list, just remove the last one 
                used[i] = false; // backtrack recovery for other elements on this level.
            }
        }
    }
}

 

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]


 

solution 1: swap swap

 

solution 2 : 

two implementations: 

 

(1)

 use a hashset to keep track of which element has been used 

example: 12345 

the first pos can be 1, 2,3,4, 5 

if we choose 2 for the first position, then the second postion can be 1, 3,4,5, 

lets say we choose 4 for the second position, then we are left with 1,3,5 for the third pos. so on .. 

time complexity is n! , n is the number of the elements in this array

 

 

(2)

use a boolean[] used to keep track of which element has been used 

example: 12345 

the first pos can be 1, 2,3,4,5 

if we choose 2 for the first position, then the second postion can be 1, 3,4,5, 

lets say we choose 4 for the second position, then we are left with 1,3,5 for the third pos. so on .. 

time complexity is n! , n is the number of the elements in this array

 

posted on 2018-07-18 07:47  猪猪&#128055;  阅读(111)  评论(0)    收藏  举报

导航