leetcode Sum系列

Britjeans發表於2018-06-03

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].

Solution

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> m;
        vector<int> res;
        if(nums.size()<2)return res;
        for(int i=0;i<nums.size();++i){
            int num=target-nums[i];
            if(m.find(num)!=m.end()){
                res.push_back(m[num]);
                res.push_back(i);
                return res;
            }
            m[nums[i]]=i;
        }
        return res;
    }
};

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]
]
Solution
vector<vector<int>> threeSum(vector<int>& num) {
    vector<vector<int> > res;
    std::sort(num.begin(), num.end());
    for (int i = 0; i < num.size(); i++) {       
        int target = -num[i];
        int front = i + 1;
        int back = num.size() - 1;
        while (front < back) {
            int sum = num[front] + num[back];
            if (sum < target)
                front++;
            else if (sum > target)
                back--;
            else {
                vector<int> triplet(3, 0);
                triplet[0] = num[i];
                triplet[1] = num[front];
                triplet[2] = num[back];
                res.push_back(triplet);     
                while (front < back && num[front] == triplet[1]) front++;
                while (front < back && num[back] == triplet[2]) back--;
            }
            
        }
        while (i + 1 < num.size() && num[i + 1] == num[i]) 
        i++;
    }
    return res;
}

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]
]

Solution

vector<vector<int>> fourSum(vector<int>& nums, int target) {    
        vector<vector<int>> res;
        if(nums.size()<4) return res;
        vector<int>tmp(4,0);
        sort(nums.begin(),nums.end());
        int front,back;
        for(int i=0;i<nums.size()-3;++i){
            tmp[0]=nums[i];
            for(int j=i+1;j<nums.size()-2;++j){
                tmp[1]=nums[j];
                front=j+1;
                back=nums.size()-1;
                int subtar=target-nums[i]-nums[j];
                while(front<back){
                    if((nums[front]+nums[back])<subtar) ++front;
                    else if((nums[front]+nums[back])>subtar) --back;
                    else{
                        tmp[2]=nums[front];
                        tmp[3]=nums[back];
                        res.push_back(tmp);
                        while(nums[front]==tmp[2]) ++front;
                        while(nums[back]==tmp[3]) --back;
                    }  
                }
                while(nums[j]==tmp[1]) ++j;
                j--;
            }
            while(nums[i]==tmp[0])++i;
            i--;
        }
        return res;
    }    




相關文章