跳过正文
  1. Posts/

leetcode 90. Subsets II (枚举子集)

·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的。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月05日 星期三 17时15分34秒
 4File Name :90.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,vector<int>& nums)
12    {
13//	cout<<"cur:"<<cur<<endl;
14	if (cur==n) //from 0
15	{
16	    vector<int>tmp;
17	    for ( int i = 0 ; i < n ; i++)
18		if (B[i]) tmp.push_back(nums[i]);
19
20	    sort(tmp.begin(),tmp.end());
21	    int siz = tmp.size();
22//	    for ( int i = 0 ; i < siz ; i++) printf("%d ",tmp[i]);printf("\n");
23	    se.insert(tmp);
24	    return;
25	}
26	B[cur] = 1;
27	get_subset(n,B,cur+1,nums);
28	B[cur] = 0;
29	get_subset(n,B,cur+1,nums);
30    }
31    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
32	int siz = nums.size();
33	if (siz==0) return res;
34	get_subset(siz,B,0,nums);
35	for ( auto &it : se)
36	{
37	    res.push_back(it);
38	}
39	return res;
40    }
41};

相关文章

106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构建二叉树)

·1 分钟
/* *********************************************** 1Author :111qqz 2Created Time :2017年04月05日 星期三 16时49分57秒 3File Name :106.cpp 4************************************************ */ 5/** 6 * Definition for a binary tree node. 7 * struct TreeNode { 8 * int val; 9 * TreeNode *left; 10 * TreeNode *right; 11 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 * }; 13 */ 14class Solution { 15public: 16 TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { 17 int siz = inorder.size(); 18 if (siz==0) return NULL; 19 int rt = postorder[siz-1]; 20 int pos = -1; 21 for ( int i = 0 ; i < siz; i++) 22 { 23 if (inorder[i]==rt) 24 { 25 pos = i ; 26 break; 27 } 28 } 29 TreeNode *head = new TreeNode(rt); 30 vector<int>in,post; 31 for ( int i = 0 ; i < pos ; i++) 32 { 33 in.push_back(inorder[i]); 34 post.push_back(postorder[i]); 35 } 36 head->left = buildTree(in,post); 37 in.clear(); 38 post.clear(); 39 for ( int i = pos + 1 ; i < siz ; i++) 40 { 41 in.push_back(inorder[i]); 42 post.push_back(postorder[i-1]); 43 } 44 head->right = buildTree(in,post); 45 return head; 46 } 47};

今日头条2017秋招笔试_1

·2 分钟
头条校招(今日头条2017秋招真题) 题目描述 头条的2017校招开始了!为了这次校招,我们组织了一个规模宏大的出题团队。每个出题人都出了一些有趣的题目,而我们现在想把这些题目组合成若干场考试出来。在选题之前,我们对题目进行了盲审,并定出了每道题的难度系数。一场考试包含3道开放性题目,假设他们的难度从小到大分别为a, b, c,我们希望这3道题能满足下列条件: