↓ 跳过正文
  1. Posts/

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

 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(根据中序和后序遍历构建二叉树)

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

leetcode 287. Find the Duplicate Number (floyd判圈算法找重复元素)

·422 字·1 分钟
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: 1. You **must not** modify the array (assume the array is read only). 2. You must use only constant, _O_(1) extra space. 3. Your runtime complexity should be less than `O(n2)`. 4. There is only one duplicate number in the array, but it could be repeated more than once. 思路:O(n^2)的复杂度暴力即可,说个O(n)复杂度的解法。

leetcode 532. K-diff Pairs in an Array (找差为k的数对)

·557 字·2 分钟
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k. Example 1: 1Input: [3, 1, 4, 1, 5], k = 2 2Output: 2 3Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5). 4Although we have two 1s in the input, we should only return the number of unique pairs. Example 2:

leetcode 448. Find All Numbers Disappeared in an Array(寻找所有消失的元素)

·341 字·1 分钟
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] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. Example: Input: [4,3,2,7,8,2,3,1] Output: [5,6] 思路:由于元素大小有限制,是在1..n之间。

leetcode 105 根据前序遍历和中序遍历重构二叉树

·433 字·1 分钟
思路: 分治搞之。 实际上两个vector就够了。。。4个会MLE(在leetcode上。。。 代码实现 1/** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10class Solution { 11public: 12 TreeNode* res; 13 TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { 14 int siz = pre.size(); 15 if (siz==0) return NULL; 16 int rt = pre[0]; 17 int pos=-1; 18 for ( int i = 0 ; i < siz; i++) 19 if (vin[i]==rt) 20 { 21 pos = i; 22 break; 23 } 24 vector<int>preL,preR,vinL,vinR; 25 for ( int i = 0 ; i < pos ; i++) 26 { 27 preL.push_back(pre[i+1]); 28 vinL.push_back(vin[i]); 29 } 30 for ( int i = pos + 1 ; i < siz ; i++) 31 { 32 preR.push_back(pre[i]); 33 vinR.push_back(vin[i]); 34 } 35 TreeNode *head = new TreeNode(rt); 36 head->left = reConstructBinaryTree(preL,vinL); 37 head->right = reConstructBinaryTree(preR,vinR); 38 return head; 39 40 41 } 42}; 43 44 45 46 47/* *********************************************** 48Author :111qqz 49Created Time :2017年04月05日 星期三 16时21分35秒 50File Name :105.cpp 51************************************************ */ 52/** 53 54 * Definition for a binary tree node. 55 56 * struct TreeNode { 57 58 * int val; 59 60 * TreeNode *left; 61 62 * TreeNode *right; 63 64 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 65 66 * }; 67 68 */ 69 70class Solution { 71 72public: 73 74 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { 75 76 int siz = preorder.size(); 77 if (siz==0) return NULL; 78 int rt = preorder[0]; 79 int pos; 80 for ( int i = 0 ; i < siz ; i++) 81 { 82 if (inorder[i]==rt) 83 { 84 pos = i; 85 break; 86 } 87 } 88 vector<int>preL,inL; 89 TreeNode *head = new TreeNode(rt); 90 for ( int i = 0 ; i < pos ; i++) 91 { 92 preL.push_back(preorder[i+1]); 93 inL.push_back(inorder[i]); 94 } 95 head->left = buildTree(preL,inL); 96 97 preL.clear(); 98 inL.clear(); 99 for ( int i = pos + 1 ; i < siz; i++) 100 { 101 preL.push_back(preorder[i]); 102 inL.push_back(inorder[i]); 103 } 104 head->right = buildTree(preL,inL); 105 return head; 106 107 } 108 109};