leetcode 日经贴,Cpp code -Permutations II
2015-05-06 14:43
351 查看
Permutations II
class Solution { public: bool next_permutation(vector<int> &nums) { int n = nums.size(); if (n <= 1) { return false; } int ed = n - 1; while (ed > 0 && nums[ed - 1] >= nums[ed]) { --ed; } int st = ed; if (ed > 0) { for (int i = n - 1; i >= 0; --i) { if (nums[i] > nums[ed - 1]) { ed = i; break; } } swap(nums[ed], nums[st - 1]); } ed = n - 1; bool ret = st > 0; while (st < ed) { swap(nums[st++], nums[ed--]); } return ret; } vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int> >ret; ret.push_back(nums); while (next_permutation(nums)) { ret.push_back(nums); } return ret; } };
相关文章推荐
- leetcode 日经贴,Cpp code -Delete Node in a Linked List
- leetcode 日经贴,Cpp code -Contains Duplicate
- leetcode 日经贴,Cpp code -Binary Tree Right Side View
- leetcode 日经贴,Cpp code -Largest Number
- leetcode 日经贴,Cpp code -Remove Duplicates from Sorted Array II
- leetcode 日经贴,Cpp code -Single Number
- leetcode 日经贴,Cpp code -Maximum Depth of Binary Tree
- leetcode 日经贴,Cpp code -Pow(x, n)
- leetcode 日经贴,Cpp code -Permutation Sequence
- leetcode 日经贴,Cpp code -Merge Sorted Array
- leetcode 日经贴,Cpp code -Subsets II
- leetcode 日经贴,Cpp code -Course Schedule II
- leetcode 日经贴,Cpp code -Power of Two
- leetcode 日经贴,Cpp code -Rotate Image
- leetcode 日经贴,Cpp code -Basic Calculator
- leetcode 日经贴,Cpp code -Product of Array Except Self
- leetcode 日经贴,Cpp code -Number of Islands
- leetcode 日经贴,Cpp code -Implement Stack using Queues
- leetcode 日经贴,Cpp code -Invert Binary Tree
- leetcode 日经贴,Cpp code -Plus One