leetcode题解日练--2016.9.01

###不给自己任何借口

今日题目:

1、两数和II 输入数组有序;

2、三数和

3、四数和

今日摘录:

一个人自以为刻骨铭心的回忆。别人也许早已经忘记了。

——张小娴《流波上的舞》

167. Two Sum II - Input array is sorted | Difficulty: Medium

Given an array of integers that is already sorted in ascending order, 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
tag:数组|两指针|二分
题意:字符串之间的乘法,不允许转换为整型。

思路:
1、一头一尾两指针,很简单

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int> res(2,0);
        sort(numbers.begin(),numbers.end());
        int i=0,j=numbers.size()-1;
        while(i<j)
        {
            if(numbers[i]+numbers[j]==target) break;
            else if(numbers[i]+numbers[j]>target) j--;
            else    i++;
        }
        res[0]=i+1;
        res[1]=j+1;
        return res;
    }
};

结果:16ms

15. 3Sum | Difficulty: Medium

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.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

tag:数组|两指针

题意:找到3个数字,a+b+c =0,不算重复的。
思路:
1、首先对数组进行排序,然后固定第一个数,然后在剩下的数中去查找第二个数和第三个数。

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int> >res;
        vector<int> tmp(3,0);
        int n=nums.size();
        int i,j,target;
        sort(nums.begin(),nums.end());
        for(int k=0;k<n;k++)
        {
            target = -nums[k];
            i = k+1,j=n-1;
            while(i<j)
            {
                int val = nums[i]+nums[j];
                if(val>target)  j--;
                else if(val< target)    i++;
                else
                {
                    tmp[0] = nums[i];
                    tmp[1] = nums[j];
                    tmp[2] = nums[k];
                    res.push_back(tmp);
                    while(i<j && nums[i]==tmp[0])   i++;
                    while(i<j && nums[j]==tmp[1])   j--;
                }
            }
        while (k + 1 < nums.size() && nums[k + 1] == nums[k]) 
            k++;
        }
        return res;
    }
};

结果:56ms

18. 4Sum | Difficulty: Medium

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.

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

tag:数组|哈希表|两指针

题意:找到4个数字,a+b+c =target,不算重复的。
思路:
1、首先对数组进行排序,然后固定第一个数和第二个数,然后在剩下的数中去查找第三个数和第四个数。

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

        while (i + 1 < n && nums[i + 1] == nums[i]) 
            i++;
        }
        return res;
    }
};

结果:64ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值