↓ 跳过正文
  1. Tags/

枚举子集

2017

leetcode 77. Combinations (枚举子集,限定集合大小)

·192 字·1 分钟
Given two integers n and k, return all possible combinations of k numbers out of 1 … n. 思路:就是枚举子集,根据集合的大小剪枝。。。最后只要集合大小为k的集合 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 15时25分37秒 4File Name :77.cpp 5************************************************ */ 6class Solution { 7public: 8 set<vector<int> >se; 9 int B[1005]; 10 vector<vector<int> >res; 11 void get_subset(int n,int *B,int cur,int cnt,int k) 12 { 13 if (cur==n) 14 { 15 vector<int>tmp; 16 for ( int i = 0 ; i < n ; i++) 17 if (B[i]) tmp.push_back(i+1); 18 if (tmp.size()==k) 19 se.insert(tmp); 20 return ; 21 } 22 if (cnt<k) 23 { 24 B[cur] = 1; 25 get_subset(n,B,cur+1,cnt+1,k); 26 } 27 B[cur] = 0 ; 28 get_subset(n,B,cur+1,cnt,k); 29 } 30 vector<vector<int>> combine(int n, int k) { 31 get_subset(n,B,0,0,k); 32 for (auto &it:se) 33 { 34 res.push_back(it); 35 } 36 return res; 37 } 38};

leetcode 40. Combination Sum II (枚举子集,和为定值)

·106 字·1 分钟
* Total Accepted: **106670** * Total Submissions: **329718** * Difficulty: **Medium** * Contributor: **LeetCode** Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. 题意:若干正数,求所有和为target的组合。

leetcode 90. Subsets II (枚举子集)

·266 字·1 分钟
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: 1[ 2 [2], 3 [1], 4 [1,2,2], 5 [2,2], 6 [1,2], 7 [] 8] 思路: 复习(?)一下 枚举子集的三种写法 (还有种更飘逸的…先不写了orz 这道题我用位向量法A的。。