LeetCode算法题目:Subsets AND Subsets II
2017-05-08 19:14
435 查看
题目:
Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
分析:
方法一 位操作
求子集问题就是求组合问题。数组中的n个数可以用n个二进制位表示,当某一位为1表示选择对应的数,为0表示不选择对应的数。方法二 回溯法
还可以使用深度优先搜索来遍历数组,采用回溯法来剔除元素。使用一个变量来记录路径,每遍历到一个元素即表示找到一条路径,将其加入子集中。对于数组[1,2,3]
从1开始递归查询2,3,对于2,继续向下搜索,搜索完后将2删除。
Subset II 回溯法
只需在Subsets的回溯法基础上,在回溯时去掉重复元素即可。可在其基础上加上一句while(nums[i]==nums[i+1]) i++; 即可。代码:
方法一 class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { int length=nums.size(); vector<vector<int> > result; int p=1<<length; for(int i=0;i<p;i++) { vector<int> tmp; //计算i中有那几位为1 for(int j=0;j<length;j++) { //判断i中第j位是否为1 int m=1<<j; if(i&m) { tmp.push_back(nums[j]); } } result.push_back(tmp); } return result; } };
方法二 class Solution { public: //使用深度优先的回溯法 vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> result; vector<int> path; sort(nums.begin(),nums.end()); result.push_back(path); dfs(nums,0,path,result); return result; } void dfs(vector<int>& nums,int pos,vector<int> & path,vector<vector<int>> & result) { if(pos==nums.size()) return; for(int i=pos;i<nums.size();i++) { path.push_back(nums[i]); result.push_back(path); dfs(nums,i+1,path,result); path.pop_back(); } } };
Subsets II (回溯法) class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { vector<vector<int>> result; vector<int> path; sort(nums.begin(),nums.end()); result.push_back(path); dfs(nums,0,path,result); return result; } void dfs(vector<int>& nums,int pos,vector<int> & path,vector<vector<int>> & result) { if(pos==nums.size()) return; for(int i=pos;i<nums.size();i++) { path.push_back(nums[i]); result.push_back(path); dfs(nums,i+1,path,result); path.pop_back(); while(nums[i]==nums[i+1]) i++; } } };
4000
相关文章推荐
- Leetcode Best Time to Buy and Sell Stock II OJ 刷题 算法
- Leetcode算法学习日志-122 Best Time to Buy and Sell Stock II
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
- LeetCode算法题目:Reverse Linked List II
- [leetcode 78 & 90, Medium] Subsets I and II
- LeetCode算法题目:Populating Next Right Pointers in Each Node II
- 【python】【leetcode】【算法题目167—Two Sum II - Input array is sorted】
- LeetCode题解:Subsets I and II
- c++ leetcode 算法题 Best Time to Buy and Sell Stock II
- LeetCode Online Judge 题目C# 练习 - Reverse Linked List II
- LeetCode Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II Solution
- LeetCode Online Judge 题目C# 练习 - Binary Tree Level Order Traversal II
- [leetcode] best time to buy and sell stocks II
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- LeetCode Online Judge 题目C# 练习 - Sparial Matrix II