↓ 跳过正文
  1. Posts/

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

·224 字·1 分钟

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

思路: 排序,然后two pointer,复杂度 O(n^2)

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 16时24分28秒
 4File Name :16.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    int n;
11    int threeSumClosest(vector<int>& nums, int target) {
12	n = nums.size();
13	sort(nums.begin(),nums.end());
14	int mn = 0x3f3f3f3f;
15	int x,y,z;
16	for ( int i = 0 ; i <=n-3 ; i++)
17	{
18	    int head = i+1;
19	    int tail = n-1;
20	    int tar = target - nums[i];
21	    while (head<tail)
22	    {
23		int cur = target-nums[i]-nums[head]-nums[tail];
24		if (abs(cur)<mn)
25		{
26		    mn = abs(cur);
27		    x = nums[i];
28		    y = nums[head];
29		    z = nums[tail];
30		}
31		if (nums[head]+nums[tail]==tar) return target;
32		if (nums[head]+nums[tail]<tar) head++;
33		else tail--;
34	    }
35	}
36	return x + y + z;
37
38
39
40
41    }
42
43};

相关文章

leetcode 18. 4Sum (k-sum问题,two pointer)

·274 字·1 分钟
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. 思路: O(n^2)枚举两个元素,变成2-sum问题,总体复杂度O(n^3)

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)

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 解题报告 中的算法搞一下就好了。。