程序员面试题目总结--数组(27)【Two Sum 、3Sum、3Sum Closest 、4Sum】
2014-08-04 11:11
453 查看
27、Two Sum
题目:给定一个整数数组,找出和为指定数值的两个数,返回这两个数的下标,要求下标以1为基准
如 Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
原题:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please
note that your returned answers (both index1 and index2) are not
zero-based. You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
分析:hash法,用一个哈希表,存储每个数对应的下标,复杂度O(n).
题目:给定一个整型数组,找出这个数组中和为0的三个数,要求找出所有符合条件的数
如 given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
原题: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:
• Elements in a triplet (a; b; c) must be in non-descending order. (ie, a<= b<=c)
• The solution set must not contain duplicate triplets.
For example, given array S = {-1,0,1,2,-1,-4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
分析:先排序,然后左右夹逼,复杂度O(n2)。这个方法可以推广到k-Sum,先排序,然后做k-2次循环,在最内层循环左右夹逼
扩展2:3Sum Closest
题目:给定一个整型数组,找出和最接近指定数值的三个数,返回三个数的和
原题: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.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
分析:先排序,然后左右夹逼,复杂度O(n2)
扩展3:4Sum
题目: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:
• Elements in a quadruplet (a; b; c; d) must be in non-descending order. (ie, a b c d)
• The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
分析:
方法一:先排序,然后左右夹逼,时间复杂度O(n^3),空间复杂度O(1)
方法二:map 做缓存。用一个hashmap 先缓存两个数的和时间复杂度,平均O(n^2),最坏O(n^4),空间复杂度O(n^2)
题目:给定一个整数数组,找出和为指定数值的两个数,返回这两个数的下标,要求下标以1为基准
如 Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
原题:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please
note that your returned answers (both index1 and index2) are not
zero-based. You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
分析:hash法,用一个哈希表,存储每个数对应的下标,复杂度O(n).
#include<iostream> #include <hash_map> #include <vector> #include <algorithm> using namespace std; using stdext::hash_map; vector<int> twoSum(vector<int> &num, int target) { hash_map<int, int> mapping; vector<int> result; for (int i = 0; i < num.size(); i++) { mapping[num[i]] = i; } for (int i = 0; i < num.size(); i++) { const int gap = target - num[i]; if (mapping.find(gap) != mapping.end()) { result.push_back(i + 1); result.push_back(mapping[gap] + 1); break; } } return result; }扩展1:3Sum
题目:给定一个整型数组,找出这个数组中和为0的三个数,要求找出所有符合条件的数
如 given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
原题: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:
• Elements in a triplet (a; b; c) must be in non-descending order. (ie, a<= b<=c)
• The solution set must not contain duplicate triplets.
For example, given array S = {-1,0,1,2,-1,-4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
分析:先排序,然后左右夹逼,复杂度O(n2)。这个方法可以推广到k-Sum,先排序,然后做k-2次循环,在最内层循环左右夹逼
vector<vector<int> > threeSum(vector<int>& num) { vector<vector<int> > result; if (num.size() < 3) return result; sort(num.begin(), num.end()); const int target = 0; vector<int>::iterator last = num.end(); for (vector<int>::iterator a = num.begin(); a < last; ++a) { vector<int>::iterator b = a+1; vector<int>::iterator c = last-1; while (b < c) { if (*a + *b + *c < target) { ++b; } else if (*a + *b + *c > target) { --c; } else { vector<int> tmp; tmp.push_back(*a); tmp.push_back(*b); tmp.push_back(*c); result.push_back(tmp); ++b; --c; } } } sort(result.begin(), result.end()); result.erase(unique(result.begin(), result.end()), result.end()); return result; }
扩展2:3Sum Closest
题目:给定一个整型数组,找出和最接近指定数值的三个数,返回三个数的和
原题: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.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
分析:先排序,然后左右夹逼,复杂度O(n2)
int threeSumClosest(vector<int>& num, int target) { int result = 0; int min_gap = INT_MAX; sort(num.begin(), num.end()); vector<int>::iterator last = num.end(); for (vector<int>::iterator a = num.begin(); a != last; ++a) { vector<int>::iterator b = a+1; vector<int>::iterator c = last-1; while (b < c) { const int sum = *a + *b + *c; const int gap = abs(sum - target); if (gap < min_gap) { result = sum; min_gap = gap; } if (sum < target) ++b; else --c; } } return result; }
扩展3:4Sum
题目: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:
• Elements in a quadruplet (a; b; c; d) must be in non-descending order. (ie, a b c d)
• The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
分析:
方法一:先排序,然后左右夹逼,时间复杂度O(n^3),空间复杂度O(1)
vector<vector<int> > fourSum(vector<int>& num, int target) { vector<vector<int> > result; if (num.size() < 4) return result; sort(num.begin(), num.end()); vector<int>::iterator last = num.end(); for (vector<int>::iterator a = num.begin(); a < last; ++a) { for (vector<int>::iterator b = a+1; b < last; ++b) { vector<int>::iterator c = b+1; vector<int>::iterator d = last-1; while (c < d) { if (*a + *b + *c + *d < target) { ++c; } else if (*a + *b + *c + *d > target) { --d; } else { vector<int> tmp; tmp.push_back(*a); tmp.push_back(*b); tmp.push_back(*c); tmp.push_back(*d); result.push_back(tmp); ++c; --d; } } } } sort(result.begin(), result.end()); result.erase(unique(result.begin(), result.end()), result.end()); return result; }
方法二:map 做缓存。用一个hashmap 先缓存两个数的和时间复杂度,平均O(n^2),最坏O(n^4),空间复杂度O(n^2)
vector<vector<int> > fourSum1(vector<int> &num, int target) { vector<vector<int> > result; if (num.size() < 4) return result; sort(num.begin(), num.end()); hash_map<int, vector<pair<int, int> > > cache; for (size_t a = 0; a < num.size(); ++a) { for (size_t b = a + 1; b < num.size(); ++b) { cache[num[a] + num[b]].push_back(pair<int, int>(a, b)); } } for (int c = 0; c < num.size(); ++c) { for (size_t d = c + 1; d < num.size(); ++d) { const int key = target - num[c] - num[d]; if (cache.find(key) == cache.end()) continue; const vector<pair<int, int> >& vec = cache[key]; for (size_t k = 0; k < vec.size(); ++k) { if (c <= vec[k].second) continue; // 有重叠 vector<int> tmp; tmp.push_back(num[vec[k].first]); tmp.push_back(num[vec[k].second]); tmp.push_back(num[c]); tmp.push_back(num[d]); result.push_back(tmp); } } } sort(result.begin(), result.end()); result.erase(unique(result.begin(), result.end()), result.end()); return result; }
相关文章推荐
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- 程序员面试题目总结--数组(五)【数组的后面m个数移动为前面m个数、列的前n项数据、判断整数x是否可以表示成n个连续正整数的和、数组中出现奇数次的元素、二维数组中的查找】
- 程序员面试题目总结--数组(一)【递归求数组所有元素和、用一个for循环打印出一个二维数组、用递归判断数组是否是递增、有序数组中删除重复元素】
- 程序员面试题目总结--数组(四)【数列中符合条件数对的个数、数组是否存在重复元素、重新排列数组使数组左边为奇数,右边为偶数、数组中的第二大数、数组中的最小值和最大值】
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- 程序员面试题目总结--链表(2)【找出单链表中的倒数第K个元素】
- 程序员面试题目总结--链表(6)【单链表排序】
- Android开发之数组类的面试题目,android工程师java程序员必备
- 数组之2Sum,3Sum,4Sum,3Sum closest总结
- Two Sum,3Sum,3Sum Closest,4Sum
- 程序员面试题目总结--链表(4)【从尾到头输出单链表】
- 对程序员面试题目的重要总结
- 程序员面试题目总结--链表(1)【单链表插入和删除】
- leetcode做题总结,题目3Sum,4Sum,3Sum Closest---- 2012/01/16-17-26
- 第五周:[leetcode] Two Sum、3Sum、3Sum Closest、4Sum
- 程序员面试题目总结--链表(5)【寻找单链表的中间结点】
- 程序员面试题目总结--链表(3)【单链表反转】
- [Leetcode][求和问题2Sum/3Sum/4Sum/KSum]相关题目汇总/分析/总结
- CSDN社区分享面试经历活动作品27——java程序员求职总结
- [置顶] Android开发之数组类的面试题目,android工程师java程序员必备