LeetCode 46. Permutations

原题链接在这里:https://leetcode.com/problems/permutations/

题目:

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

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

题解:

For recursion, first figure out what parameters are needed in each state.

Here it needs nums array, boolean array used to track visited before, item and result.

From head of array, get the first num not visited before, mark it as visited, add it to item. Then dfs to next state.

Backtracking needs to revert, remove num from item and mark it not visited.

Stop condition, item size is same as nums length.  

Time Complexity: exponential.

Space: O(nums.length). regardless res.

AC Java:

 1 public class Solution {
 2     public List<List<Integer>> permute(int[] nums) {
 3         List<List<Integer>> res = new ArrayList<List<Integer>>();
 4         if(nums == null || nums.length == 0){
 5             return res;
 6         }
 7         boolean [] used = new boolean[nums.length];
 8         helper(nums,used,new ArrayList<Integer>(), res);
 9         return res;
10     }
11     private void helper(int[] nums, boolean [] used, List<Integer> item, List<List<Integer>> res){
12         if(item.size() == nums.length){
13             res.add(new ArrayList<Integer>(item));
14             return;
15         }
16         for(int i = 0; i<nums.length; i++){
17             if(!used[i]){
18                 used[i] = true;
19                 item.add(nums[i]);
20                 helper(nums,used,item,res);
21                 item.remove(item.size()-1);
22                 used[i] = false;
23             }
24         }
25     }
26 }

AC Python:

 1 class Solution(object):
 2     def permute(self, nums):
 3         res = []
 4         self.dfs(nums, [False] * len(nums), [], res)
 5         return res
 6         
 7     def dfs(self, nums, used, item, res):
 8         if(len(item) == len(nums)):
 9             res.append(item[:])
10             return
11         
12         for i in range(len(nums)):
13             if not used[i]:
14                 used[i] = True
15                 item.append(nums[i])
16                 self.dfs(nums, used, item, res)
17                 item.pop()
18                 used[i] = False

这道题的迭代方法如下:

Subsets相似,开始时item先加nums[0], 把item加到res里.

然后每次添加新的nums[i], 首先把res里的每一个item拿出来, 用cur表示.

在cur的所有可能位置加上新的元素nums[i], 然后把它加载回res里。

Note: res原有的item不能保留,所以每次扫描res所有item前新建newRes, 添加完新元素nums[i]的item是要加到newRes中去的,所有可能的item都加完后再把newRes赋值回res去。

Time Complexity: exponential.

Space: O(n!). n = nums.length.

AC Java:

 1 public class Solution {
 2     public List<List<Integer>> permute(int[] nums) {
 3         List<List<Integer>> res = new ArrayList<List<Integer>>();
 4         if(nums == null || nums.length == 0){
 5             return res;
 6         }
 7         List<Integer> item = new ArrayList<Integer>();
 8         item.add(nums[0]);
 9         res.add(item);
10         
11         for(int i = 1; i<nums.length; i++){
12             List<List<Integer>> newRes = new ArrayList<List<Integer>>();
13             for(int j = 0; j<res.size(); j++){
14                 List<Integer> cur = res.get(j);
15                 for(int k = 0; k<=cur.size(); k++){
16                     // 记得这里要做个copy, 不能直接在原来的cur上加
17                     item = new ArrayList<Integer>(cur);
18                     item.add(k, nums[i]);
19                     newRes.add(item);
20                 }
21             }
22             res = newRes;
23         }
24         return res;
25     }
26 }

类似CombinationsLetter Tile Possibilities.

跟上Permutations II.

posted @ 2015-09-27 11:51  Dylan_Java_NYC  阅读(761)  评论(0编辑  收藏  举报