Subsets

这题不好想,可参考:http://www.cnblogs.com/felixfang/p/3775712.html,思路很清楚。同理这种解法也满足有重复元素的情况。

 vector<vector<int>> subsets( vector<int> &S)
      {
          vector<vector<int>> res;
          vector<int> tmp;
          res.push_back(tmp);
          sort(S.begin(), S.end());

          if (S.size() == 0)return res;
          for (vector<int>::iterator ind = S.begin(); ind != S.end(); ind++)
          {
              int size = res.size();
              for (int i = 0; i < size; i++)
              {
                  vector<int> v;
                  for (vector<int>::iterator j = res[i].begin(); j != res[i].end(); j++)
                  {
                      v.push_back(*j);
                  }
                  v.push_back(*ind);
                  res.push_back(v);
              }
          }

          return res;
      }
View Code

 

posted @ 2016-07-16 15:49  牧马人夏峥  阅读(127)  评论(0编辑  收藏  举报