↓ 跳过正文
  1. Posts/

leetcode 15. 3Sum (k-sum问题,two pointer)

·251 字·1 分钟

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

思路:排序O(nlgn),然后枚举一个元素O(n),对于每个元素,在剩下的区间中 two pointer O(n)

整体复杂度 O(n^2)。

hash的解法以后补。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 16时07分00秒
 4File Name :15.cpp
 5************************************************ */
 6class Solution {
 7public:
 8    set<vector<int> >se;
 9    vector<vector<int> >res;
10    vector<vector<int>> threeSum(vector<int>& nums) {
11	sort(nums.begin(),nums.end());
12	int n = nums.size();
13	for ( int i = 0 ; i <=n-3 ; i++)
14	{
15	    int head = i+1;
16	    int tail = n-1;
17	    int tar = -nums[i];
18	    while (head<tail)
19	    {
20		if (nums[head]+nums[tail]==tar)
21		{
22		    vector<int>tmp;
23		    tmp.push_back(nums[i]);
24		    tmp.push_back(nums[head]);
25		    tmp.push_back(nums[tail]);
26		    se.insert(tmp);
27		    head++;
28		    tail--;
29		}
30		else if (nums[head]+nums[tail]<tar) head++;
31		else tail--;
32	    }
33	}
34	for ( auto &it : se)
35	    res.push_back(it);
36	return res;
37    }
38};

相关文章

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 60. Permutation Sequence (求第k个排列)

·326 字·1 分钟
The set [1,2,3,…,_n_] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): 1. `"123"` 2. `"132"` 3. `"213"` 4. `"231"` 5. `"312"` 6. `"321"` Given n and k, return the _k_th permutation sequence. Note: Given n will be between 1 and 9 inclusive. 思路:还是根据leetcode 31 解题报告 中的算法搞一下就好了。。

leetcode 47. Permutations II (生成全排列,有重复元素)

·294 字·1 分钟
Given a collection of numbers that might contain duplicates, return all possible unique permutations.__ 思路:和leet code 46 类似,最后用set去个重即可。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 15时00分48秒 4File Name :47.cpp 5************************************************ */ 6class Solution { 7 8public: 9 void solve( vector<int>&nums) 10 { 11 int n = nums.size(); 12 if (n==0) return; 13 int k = -1; 14 for ( int i = n-2 ; i >= 0 ; i--) 15 { 16 if (nums[i]<nums[i+1]) 17 { 18 k = i; 19 break; 20 } 21 } 22 if (k==-1) 23 { 24 reverse(nums.begin(),nums.end()); 25 return; 26 } 27 int l = -1; 28 for ( int i = n-1 ; i >k ; i--) 29 { 30 if (nums[k]<nums[i]) 31 { 32 l = i; 33 break; 34 } 35 } 36 swap(nums[l],nums[k]); 37 reverse(nums.begin()+k+1,nums.end()); 38 } 39 40 void pr (vector<int> &nums) 41 { 42 int siz = nums.size(); 43 for ( int i = 0 ; i < siz; i++) 44 printf("%d%c",nums[i],i==siz-1?'\n':' '); 45 } 46 vector<vector<int>> permuteUnique(vector<int>& nums) { 47 set<vector<int> >se; 48 vector<vector<int> >res; 49 int n = nums.size(); 50 int total = 1 ; 51 for ( int i = 2 ; i <= n ; i++) total*=i; 52 53 for ( int i = 1 ; i <= total ; i++) 54 { 55 se.insert(nums); 56// pr(nums); 57 solve(nums); 58 } 59 for ( auto &it :se) 60 { 61 res.push_back(it); 62 } 63 return res; 64 } 65 66};

leetcode 46. Permutations (生成全排列,无重复元素)

·248 字·1 分钟
Given a collection of distinct numbers, return all possible permutations. 思路:调用n-1次 leetcode 31 解题报告 中提到的算法即可。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月13日 星期四 14时49分34秒 4File Name :46.cpp 5 ************************************************ */ 6class Solution { 7 8 public: 9 10 void solve(vector<int> &nums) 11 { 12 int n = nums.size(); 13 if (n==0) return; 14 int k = -1; 15 for ( int i = n-2 ; i >= 0 ; i--) 16 { 17 if (nums[i]<nums[i+1]) 18 { 19 k = i ; 20 break; 21 } 22 } 23 if (k==-1) 24 { 25 reverse(nums.begin(),nums.end()); 26 return; 27 } 28 int l = -1; 29 for ( int i = n-1 ; i > k ; i-- ) 30 { 31 if (nums[k]<nums[i]) 32 { 33 l = i ; 34 break; 35 } 36 } 37 swap(nums[l],nums[k]); 38 reverse(nums.begin()+k+1,nums.end()); 39 } 40 vector<vector<int>> permute(vector<int>& nums) { 41 vector<vector<int> >res; 42 int n = nums.size(); 43 int total = 1; 44 for ( int i = 2 ; i <= n ; i++) total*=i; 45 for ( int i = 1 ; i <= total; i++) 46 { 47 res.push_back(nums); 48 solve(nums); 49 } 50 51 return res; 52 } 53 54};