46、47-Permutations
来源:互联网 发布:java 格式化html 编辑:程序博客网 时间:2024/05/16 11:51
难度:medium
题目描述
46-Permutations I:
47-Permutations II:
算法分析
46:
直接使用函数next_permutation(_vector.begin(), _vector.end())
47:
在46题的基础上,使用二层循环,对得到的全排列两两进行比较,删除重复的排列
代码实现
46:
// use function next_permutationclass Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; sort(nums.begin(), nums.begin()+nums.size()); do { result.push_back(nums); } while (next_permutation(nums.begin(), nums.begin() + nums.size())); return result; }};
47:
class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { vector<vector<int>> result; sort(nums.begin(), nums.begin()+nums.size()); do { result.push_back(nums); } while (next_permutation(nums.begin(), nums.begin() + nums.size())); int n = result.size(); for (int i = 0; i < result.size(); ++i) { for (int j = i + 1; j < result.size(); ++j) { if (isSame(result[i], result[j])) result.erase(result.begin() + j); } } return result; } bool isSame(vector<int> a, vector<int> b) { int n = a.size(); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) return false; } return true; }};
阅读全文
0 0
- Leetcode 46/47 Permutations, Permutations II
- leetcode 46-Permutations and 47-Permutations II
- LeetCode 46 Permutations + LeetCode 47 Permutations II
- 46(47). Permutations
- 46、47-Permutations
- 46, 47 Permutations I, II
- 46 permutations && 47 Permutation II
- 46 Permutations
- 46 Permutations
- 46Permutations
- [46]Permutations
- Permutations 46
- [LeetCode 46 & 47]Permutations I & II
- <LeetCode OJ> 47/46 Permutations (II / I)
- (Leetcode)46&47 Permutations--LinkedList and HashSet
- [Leetcode] #46#47 Permutations I & II
- [leetcode 47] Permutations II
- leetcode || 47、 Permutations II
- 415错误及解决方法
- eclipse Build Automatically在哪里
- nginx启动
- logistic回归中的梯度下降法怎么来的
- lastmonthexam——DrawerLayout+TabLayout+ViewPager+PullToRefresh+Fragment+TypeTkken
- 46、47-Permutations
- 最全centos源
- iPhone X 适配 ( iOS 11适配 ) 打理刘海
- Maven工具
- HTK语音识别中的决策树
- 剑指offer—数组中的逆序对
- 程序设计入门2 关于scanf()大彻大悟
- 收藏的
- Code Control MVC框架-创建一个Controller