• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Subsets II

这题自己没想出来看了网上答案,才知道只要在上一题的基础上改用set而不用vector就可以了,set和vector的区别就在于set里没有重复元素,只要用set.insert(*), 会自动识别是否重复。set好牛逼。。

 1 class Solution {
 2 public:
 3     void dfs(int cur, int n, set<vector<int>> &T, vector<int> &tmp, vector<int> S) {
 4         if (cur == n) {
 5             T.insert(tmp);
 6             return;
 7         }
 8         dfs(cur+1, n, T, tmp, S);
 9         tmp.push_back(S[cur]);
10         dfs(cur+1, n, T, tmp, S);
11         tmp.pop_back();
12     }
13     vector<vector<int> > subsetsWithDup(vector<int> &S) {
14         // Start typing your C/C++ solution below
15         // DO NOT write int main() function
16         sort(S.begin(), S.end());
17         set<vector<int>> T;
18         vector<vector<int>> ret;
19         if (!S.size()) return ret;
20         vector<int> tmp;
21         dfs(0, S.size(), T, tmp, S);
22         for (set<vector<int>>::iterator it = T.begin(); it != T.end(); it++)
23             ret.push_back(*it);
24         return ret;
25     }
26 };

 

posted @ 2013-04-21 12:51  ying_vincent  阅读(165)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3