Leetcode 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].

 

一. 广度优先搜索BFS

这种方法有些像广度优先搜索BFS,扩展所有可能

 1 public class Solution {
 2     public List<List<Integer>> permute(int[] nums) {
 3         int len = nums.length;
 4         List<List<Integer>> ans = new ArrayList<List<Integer>>();
 5         ans.add(new ArrayList<Integer>());
 6         for(int i = 0; i < len; i++)
 7         {
 8             List<List<Integer>> new_ans = new ArrayList<List<Integer>>();
 9             for(int j = 0; j <= i; j++)
10             {
11                 for(int k = 0; k < ans.size(); k++)
12                 {
13                    List<Integer> temp = new ArrayList<Integer>(ans.get(k));
14                    temp.add(j,nums[i]);
15                    new_ans.add(temp);
16                 }
17             }
18             ans = new_ans;
19         }
20         return ans;
21     }
22 
23 
24 }

 类似的做法还有手机键盘字母组合问题

 Letter Combinations of a Phone Number

https://leetcode.com/discuss/11261/my-iterative-sollution-very-simple-under-15-lines

二. 回溯backtracking 

这道题的回溯算法很典型

 1 class Solution {
 2 public:
 3     vector<vector<int> > permute(vector<int>& nums) {
 4         vector<vector<int> > result;
 5         if(nums.size() == 0)
 6             return result;        
 7         permutation(nums, 0, result);
 8         return result;
 9     }
10     void permutation(vector<int>& nums, int index, vector<vector<int> >& result)
11     {
12         int len = nums.size();
13         if(index == len)
14             result.push_back(nums);
15         else{
16             for(int i = index; i < len; i++){
17                 swap(nums[i], nums[index]);
18                 permutation(nums, index+1, result);
19                 swap(nums[i], nums[index]); //palindrome
20             }
21         }
22     }
23 };

 对于backtracking(or DFS)来讲,可以对比 

Palindrome Partitioning

问题

 

 1 class Solution {
 2 public:
 3     vector<vector<string> > partition(string s) {
 4         vector<vector<string> > result;
 5         vector<string> temp;
 6         dfs(s, 0, temp, result);
 7         return result;
 8     }
 9     void dfs(string &s, int index, vector<string> temp, vector<vector<string> > &result)
10     {
11         if(index == s.size())
12         {
13             result.push_back(temp);
14             return;
15         }
16 
17         for(int i = index; i < s.size(); i++)
18         {
19             string sub_str = s.substr(index, i - index + 1);
20             if(isPalindrome(sub_str))
21             {
22                 temp.push_back(sub_str);
23                 dfs(s, i+1, temp, result);
24                 temp.pop_back();   //back last, recover enviroment
25             }
26         }
27     }
28     bool isPalindrome (string s){
29         if(s.empty())
30             return false;
31         int begin = 0, end = s.length() - 1;
32         while(begin <= end)
33         {
34             if(s[begin++] != s[end--])
35                 return false;
36         }
37         return true;
38     }
39 };

 

posted on 2016-04-27 17:16  yuanww  阅读(461)  评论(0编辑  收藏  举报

导航