[LeetCode] Permutations

简介: Well, have you solved the nextPermutation problem? If so, your code can be used in this problem. The idea is fairly simple: sort nums in ascending o...

Well, have you solved the nextPermutation problem? If so, your code can be used in this problem. The idea is fairly simple:

  1. sort nums in ascending order, add it to res;
  2. generate the next permutation of nums using nextPermutation(), and add it to res;
  3. repeat 2 until the next permutation of nums returns to the sorted condition in 1.

The code is as follows.

A final note, the following code can be applied to the problem of Permutations II without any modification since the cases of duplicates have already been handled in nextPermutation(). If you want to learn more about nextPermutation(), please visit this solution.

 1     bool nextPermutation(vector<int>& nums) {
 2         int k = -1;
 3         for (int i = nums.size() - 2; i >= 0; i--) {
 4             if (nums[i] < nums[i + 1]) {
 5                 k = i;
 6                 break;
 7             }
 8         }
 9         if (k == -1) {
10             sort(nums.begin(), nums.end());
11             return false;
12         }
13         int l = -1;
14         for (int i = nums.size() - 1; i > k; i--) {
15             if (nums[i] > nums[k]) {
16                 l = i;
17                 break;
18             }
19         }
20         swap(nums[k], nums[l]);
21         reverse(nums.begin() + k + 1, nums.end());
22         return true;
23     }
24     vector<vector<int>> permute(vector<int>& nums) {
25         vector<vector<int> > res;
26         sort(nums.begin(), nums.end());
27         res.push_back(nums);
28         while (nextPermutation(nums))
29             res.push_back(nums);
30         return res;
31     }

Of course, you are supposed to use the backtracking idea. A simple code is as follows.

 1     void permutate(vector<int> nums, int start, vector<vector<int> >& res) {
 2         if (start == nums.size()) {
 3             res.push_back(nums);
 4             return;
 5         }
 6         for (int i = start; i < nums.size(); i++) {
 7             swap(nums[i], nums[start]);
 8             permutate(nums, start + 1, res);
 9         }
10     }
11 
12     vector<vector<int> > permute(vector<int>& nums) {
13         vector<vector<int> > res;
14         permutate(nums, 0, res);
15         return res;
16     }

 

目录
相关文章
|
人工智能
LeetCode 47. Permutations II
给定一组可能有重复元素不同的整数,返回所有可能的排列(不能包含重复)。
174 0
LeetCode 47. Permutations II
LeetCode 46. Permutations
给定一组不同的整数,返回所有可能的排列。
186 0
LeetCode - 46. Permutations
46. Permutations  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组,求这个数组的全排列.
991 0
LeetCode - 47. Permutations II
47. Permutations II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组(元素可能重复),求这个数组的全排列.
918 0
[LeetCode] Permutations II
Well, have you solved the nextPermutation problem? If so and you have handled the cases of duplicates at that problem, your code can be used in this problem.
788 0
LeetCode:Permutations(求全排列)
Given a collection of 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]. 思路:将元素一个一个的插入,首先只有一个元素{1},此时,插入之后会的到两个vector,{1,2},{2,1},然后继续插入第三个元素3,会得到{3,1,2},{1,3,2},{1,2,3}和{3,2,1},{2,3,1},{2,1,3}。
855 0
[LeetCode]--46. 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],
1317 0