1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
提示:unordered_map<int, int> numberToFind = target - nums[i]
答案:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
unordered_map<int, int> hash;
for(int i = 0; i < nums.size(); i++){
int numberToFind = target - nums[i];
if(hash.find(numberToFind) != hash.end()){
result.push_back(hash[numberToFind]);
result.push_back(i);
}
hash[nums[i]] = i;
}
return result;
}
};
11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
提示:当左边高度小于等于 h i++。当右边高度小于等于h j--。
答案:
class Solution {
public:
int maxArea(vector<int>& height) {
int water = 0;
int i = 0, j = height.size() - 1;
while(i < j){
int h = min(height[i], height[j]);
water = max(water, (j - i) * h);
while(height[i] <= h && i < j) i++;
while(height[j] <= h && i < j) j--;
}
return water;
}
};
15. 3Sum
Given an array nums
of n integers, are there elements a, b, c in nums
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.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
提示:sort target = -nums[i] start++ end--
答案:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
if(nums.size()<3)
return result;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size()-2; i++){
if(i > 0 && nums[i] == nums[i - 1]){
continue;
}
int start = i + 1, end = nums.size() - 1;
int target = -nums[i];
while(start < end){
if (start > i + 1 && nums[start - 1] == nums[start]) {
start++;
continue;
}
if(nums[start] + nums[end] < target){
start++;
}else if(nums[start] + nums[end] > target){
end--;
}else{
vector<int> triple;
triple.push_back(nums[i]);
triple.push_back(nums[start]);
triple.push_back(nums[end]);
result.push_back(triple);
start++;
}
}
}
return result;
}
};
16. 3Sum Closest
Given an array nums
of n integers and an integer target
, find three integers in nums
such that the sum is closest to target
. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example:
Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
提示:sort start++ end--
答案:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
if(nums.size() <= 3) {
return accumulate(nums.begin(), nums.end(), 0);
}
sort(nums.begin(), nums.end());
int res = nums[0] + nums[1] + nums[2];
for(int i = 0; i < nums.size() - 2; i++){
int j = i + 1, k = nums.size() - 1;
while(j < k){
int sum = nums[i] + nums[j] + nums[k];
if(abs(sum - target) < abs(res - target)) res = sum;
if(sum < target) j++;
else k--;
}
}
return res;
}
};
18. 4Sum
Given an array nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
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.
Example:
Given array nums = [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]]
提示:类似3sum,这次要多枚举一个 i, j, left, right. 还要注意避免重复的答案
答案:
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
if(nums.size() < 4)
return result;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size() - 3; i++){
if(i && nums[i] == nums[i - 1]) continue;
for(int j = i + 1; j < nums.size() - 2; j++){
if(j != i + 1 && nums[j] == nums[j - 1]) continue;
int sum = target - nums[i] - nums[j];
int left = j + 1, right = nums.size() - 1;
while(left < right){
if(nums[left] + nums[right] < sum){
left++;
}else if(nums[left] + nums[right] > sum){
right--;
}else{
vector<int> quadruplets;
quadruplets.push_back(nums[i]);
quadruplets.push_back(nums[j]);
quadruplets.push_back(nums[left]);
quadruplets.push_back(nums[right]);
result.push_back(quadruplets);
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) left++;
while (left < right && nums[right] == nums[right + 1]) right--;
}
}
}
}
return result;
}
};