1. 程式人生 > >LeetCode(Subsets)找出一個集合的所有子集

LeetCode(Subsets)找出一個集合的所有子集

題目要求:

Given a set of distinct integers, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
程式碼:
class Solution {
public:
  vector<vector<int> > subsets(vector<int> &S) {
    vector<vector<int> > ans;
    vector<int> path;
    ans.push_back(path);
    sort(S.begin(), S.end());
    for(size_t i = 1; i <= S.size(); ++i)
    {
      path.clear();
      DFS(S, ans, path, 0, 0, i);
    }
    return ans;
  }
  
  void DFS(vector<int>& str, vector<vector<int> >& ans, vector<int>& path,
           int start, int count, int max_count)
  {
    if(count == max_count)
    {
      ans.push_back(path);
      return ;
    }
    for(size_t i = start; i < str.size(); ++i)
    {
      path.push_back(str[i]);
      DFS(str, ans, path, i + 1, count + 1, max_count);
      path.pop_back();
    }
  }
  
};


相關推薦

LeetCode(Subsets)一個集合所有子集

題目要求: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order.The

leetcode 448 陣列中所有消失的數字

給定一個範圍在  1 ≤ a[i] ≤ n ( n = 陣列大小 ) 的 整型陣列,陣列中的元素一些出現了兩次,另一些只出現一次。 找到所有在 [1, n] 範圍之間沒有出現在陣列中的數字。 您能在不使用額外空間且時間複雜度為O(n

利用Java實現一個資料夾中所有的以某個字尾名命名的所有檔案

已經自學Java一個多月了,一直沒有利用Java實現自己的需求,直到這次,編寫Java程式,實現自己的需求,很有成就感。當然不會像程式設計大神一樣優秀,可是我會慢慢努力的。 因為需要在VS上配置PCL的環境,需要把PCL檔案中的以 ".lib" 結尾的檔案找出來,PCL庫檔

LeetCode:5. Longest Palindromic Substring(一個字串中最大的子迴文串)

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of sis 1000. Example 1: Input

LeetCode:647. Palindromic Substrings(字串中所有的迴文子串)

      Given a string, your task is to count how many palindromic substrings in this string.       The substrings with

LeetCode】442. Find All Duplicates in an Array 陣列中所有重複項

題目: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot

一個字串的所有anagram

給定一個字串集合S和一個字串str。要求設計一個數據結構,能夠快速找出集合S裡所有的、是str的anagram的字串。 思路: 首先,要分析什麼樣的字串才可以互稱為anagram。兩個字串如果是anagram,它們經過排序後得到的兩個字串一定相等。或者,兩個字串如果是ana

[LeetCode] Find All Duplicates in an Array 陣列中所有重複項

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that ap

[LeetCode] Find All Numbers Disappeared in an Array 陣列中所有消失的數字

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] in

[LeetCode] Find All Anagrams in a String 字串中所有的變位詞

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the l

LeetCode-求一個集合子集

題目描述:給定一組不含重複元素的整數陣列 nums,返回該陣列所有可能的子集(冪集)。說明:解集不能包含重複的子集。示例:輸入: nums = [1,2,3]輸出:[  [3],  [1],  [2],  [1,2,3],  [1,3],  [2,3],  [1,2],  [

一個集合所有可能的子集

【1】增量構造法 一次選出一個元素放到集合中 <pre name="code" class="cpp">#include<iostream> #include<vecto

linux C 遞迴一個路徑下的所有檔案

#include <stdio.h> #include <dirent.h> #include <stdlib.h> #include <string.h&g

C++一個二維陣列中的鞍點,即該位置上的元素在該行上最大,在該列上最小(也可能沒有鞍點)

今日正式用csdn部落格記錄,回顧我所學到的知識,分享一些我的人生感悟和自身經歷。也希望未來通夠過此平臺和更多喜愛程式設計的人交流學習。 道聽途說再加上自己的感悟,認為程式設計最重要的是思想,而不是語言本身,語言只是個工具。所以我們得先學思想。遇到問題,應該先想如果是自己去做會怎麼處理,但我們不

一個int陣列中僅出現過一次的數字(前提:只有一個這樣的數)

如: int[] num = new int[8] { -105, 2, 3, 2, -105, 3, 4, 3 }    

JAVAList集合中重複次數最多的資料和次數

import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Ent

C++之bool型別,名稱空間的練習——使用一個函式一個整型陣列中的最大值或最小值

#include<iostream> using namespace std; int findMaxOrMin(int * n,int number) { int temp=n[0]; bool isMax; cin>>isMax; for(int i=1;

python 實現求一個集合子集

不能 proc subset highlight 它的 scripts list lis 也有 概要   今天偶然看到有個關於數學中集合的問題,就突發奇想的想用python實現下求一個集合的子集。 準備   我當然先要復習下,什麽是集合,什麽是子集?   比較粗獷的講法,集

【死磕演算法之1刷Leetcode】——兩個有序陣列的中位數【Median of Two Sorted Arrays】O(log(m+n))

Median of Two Sorted Arrays 題目難度:hard 題目要求: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two s