日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

      歡迎訪問 生活随笔!

      生活随笔

      當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

      编程问答

      【Lintcode】018.Subsets II

      發布時間:2023/11/30 编程问答 44 豆豆
      生活随笔 收集整理的這篇文章主要介紹了 【Lintcode】018.Subsets II 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

      題目:

      Given a list of numbers that may has duplicate numbers, return all possible subsets

      Notice

      • Each element in a subset must be in?non-descending?order.
      • The ordering between two subsets is free.
      • The solution set must not contain duplicate subsets.

      Example

      If S =?[1,2,2], a solution is:

      [[2],[1],[1,2,2],[2,2],[1,2],[] ]

      題解:

      Solution 1 ()

      class Solution { public:vector<vector<int> > subsetsWithDup(vector<int> S) {vector<vector<int> > res;vector<int> v;sort(S.begin(), S.end());Dfs(S, res, v, 0);return res;}void Dfs(vector<int> S, vector<vector<int> > &res, vector<int> &v, int pos) {res.push_back(v);for (int i = pos; i < S.size(); ++i) {if (i == pos || S[i] != S[i - 1]) {v.push_back(S[i]);Dfs(S, res, v, i + 1);v.pop_back();}}} };

        To solve this problem, it is helpful to first think how many subsets are there. If there is no duplicate element, the answer is simply 2^n, where n is the number of elements. This is because you have two choices for each element, either putting it into the subset or not. So all subsets for this no-duplicate set can be easily constructed:

      num of subset

      • (1 to 2^0) empty set is the first subset
      • (2^0+1 to 2^1) add the first element into subset from (1)
      • (2^1+1 to 2^2) add the second element into subset (1 to 2^1)
      • (2^2+1 to 2^3) add the third element into subset (1 to 2^2)
      • ....
      • (2^(n-1)+1 to 2^n) add the nth element into subset(1 to 2^(n-1))

      Then how many subsets are there if there are duplicate elements? We can treat duplicate element as a spacial element. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset. Therefore, we are given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of subset is (k1+1)(k2+1)...(kn+1). We can easily see how to write down all the subsets similar to the approach above.

      Solution 2 ()

      class Solution { public:vector<vector<int> > subsetsWithDup(vector<int> &S) {vector<vector<int> > res{{}};sort(S.begin(), S.end());for (int i = 0; i < S.size(); ) {int cnt = 0;while (cnt + i < S.size() && S[cnt + i] == S[i]) {++cnt;}int size = res.size();for (int j = 0; j < size; ++j) {vector<int> instance = res[j];for (int k = 0; k < cnt; ++k) {instance.push_back(S[i]);res.push_back(instance);}}i += cnt;}return res;} };

      Solution 3 ()

      class Solution { public:vector<vector<int> > subsetsWithDup(vector<int> &S) {vector<vector<int> > res{{}};sort(S.begin(), S.end());int size = 1;int last = !S.empty() ? S[0] : 0;for (int i = 0; i < S.size(); ++i) {if (last != S[i]) {last = S[i];size = res.size();}int newsize = res.size();for (int j = newsize - size; j < newsize; ++j) {res.push_back(res[j]);res.back().push_back(S[i]);}}return res;} };

      ?

      轉載于:https://www.cnblogs.com/Atanisi/p/6866474.html

      總結

      以上是生活随笔為你收集整理的【Lintcode】018.Subsets II的全部內容,希望文章能夠幫你解決所遇到的問題。

      如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

      主站蜘蛛池模板: 领导揉我胸亲奶揉下面 | 日韩视频区 | 国产亚洲二区 | 男女免费网站 | 免费看日韩| 日日射夜夜操 | 国产50页 | 日韩一区二区在线看 | 久久av喷吹av高潮av萌白 | 视频在线观看网站免费 | 国产一级视频 | 91激情 | 欧洲精品视频在线 | 国产特黄 | 亚洲a一区| 四虎影视免费永久大全 | 欧美日韩另类视频 | 久久久精品久久久久 | 国产精品呻吟久久 | 亚洲一区二区三区麻豆 | 国产视频1区2区 | 国精产品一区一区三区视频 | 黄色成人在线视频 | av手机天堂网 | 99毛片| 偷看洗澡一二三区美女 | 国产精品久久久久毛片大屁完整版 | 伊人丁香| 男人日女人网站 | 狠狠人妻久久久久久综合麻豆 | 夜夜草视频 | 麻豆福利在线 | 国产一区91 | 香蕉国产片 | 成人午夜sm精品久久久久久久 | 五月婷婷欧美 | 日本a级一区 | 国产精品一区二区久久国产 | 免费看毛片网站 | 三级av毛片 | 日本高清www | 欧美巨大另类极品videosbest | 国产视频二区三区 | 国产强被迫伦姧在线观看无码 | 久久久久亚洲AV成人网人人小说 | 九色婷婷| 三级a级片 | 性按摩玩人妻hd中文字幕 | 99综合色| 国产三级自拍视频 | 91肉色超薄丝袜脚交一区二区 | 999精品在线观看 | 国产乱国产乱300精品 | 精品乱码一区二区三区四区 | 另类捆绑调教少妇 | 天海翼av在线 | 成熟了的熟妇毛茸茸 | 亚洲av无码精品一区二区 | 男女h网站| 性高潮久久久久 | 国产欧美一区二区三区视频 | 中文字幕在线视频免费 | 中文理论片 | 免费在线你懂的 | 亚洲丝袜一区 | 国产精品后入内射日本在线观看 | 亚洲国产成人精品91久久久 | 老熟妇高潮一区二区高清视频 | 爱视频福利网 | 国产欧美日韩在线 | 欧美人与zoxxxx另类 | 夜夜精品一区二区无码 | 国产美女主播在线观看 | 想要视频在线 | 国产亚洲色婷婷久久99精品 | 葵司免费一区二区三区四区五区 | 一区二区自拍 | 国产在线观看第一页 | 欧洲一区二区在线观看 | 亚洲尤物在线 | 国产1区2区3区| 久久666| 国产午夜精品福利 | 亚洲精品日韩欧美 | 美国成人免费视频 | 久久精品99国产精品日本 | 我想看一级黄色片 | 夫妻毛片 | 美女av网站| 天堂va蜜桃| 日韩欧美三级在线观看 | 欧美午夜精品久久久久久人妖 | 成人在线不卡视频 | 超碰一区 | 日本一区二区三区视频在线观看 | 久久综合爱 | av中文字幕在线播放 | 午夜一区二区视频 | 日韩中文字幕在线视频 |