LeetCode167. Two Sum II - Input array is sorted

一、问题描述

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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

注意事项:返回的是索引,不是下标。

二、解题思路

其实,如果2 Sum和3 Sum的问题搞定了,这道题真的很easy。就是left和right的移动问题

三、代码实现(类似于快速排序)

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        unordered_map<int,int> result;
        vector<int> vec;
        int left = 0;
        int right = numbers.size()-1;
        while(left < right){
            int sum = numbers[left] + numbers[right];
            if(sum > target){
                right--;
            } else if(sum < target){
                left++;
            } else {
                //题目要求,索引下标加1了
                vec.push_back(left+1);
                vec.push_back(right+1);
                left++;
                right--;
            }
        }
        return vec;
    }
};

四、这道题的变种

1. 如果有多对数字的和等于target,输出两个数的乘积最小的。

#include <climits>
class Solution
{
public:
    vector<int> FindNumbersWithSum(vector<int> a,int sum)
    {
        if(a.size()<2) return vector<int>();
       
        int min_product = INT_MAX;
        int left = 0, 
        right = a.size()-1;
        int a1,a2;
        bool find_flag = false;
        while(left < right) //从两边开始往中间扫描
        {
            int cursum = a[left] + a[right];
            if(cursum > sum) right--;
            else if(cursum < sum) left++;
            else
            {
                //因为对于递增序列,一定有x*y < (x+a)*(y-a),故这里其实可以不用判断
                //从两边开始往中间扫描时,找到的第一个即为乘机最小的一对数
                if(a[left]*a[right] < min_product)
                {
                    find_flag = true;
                    min_product = a[left]*a[right];
                    a1 = a[left];
                    a2 = a[right];
                }
                left++; //继续扫描
            }
        }
        if(find_flag) return vector<int>{a1,a2};
        else return vector<int>();
       
    }
};

2. 和为sum的连续正数序列(至少包括两个数,例如:100:[9,10,11,12,13,14,15,16]/[18,19,20,21,22]...)

class Solution
{
public:
    vector<vector<int> > FindContinuousSequence(int sum)
    {
        vector<vector<int>> result;
        if(sum < 3) return result; //至少需包含两个数 
       
        int left = 1, right = 2; //序列从1开始
        while(left < right) //注意从开头开始扫描,直到left与right相遇
        {
            int cursum = (left+right)*(right - left + 1)/2; //用等差数列的求和公式
            if(cursum < sum) right++;
            else if(cursum > sum) left++;
            else
            {
                vector<int> temp;
                for(int i = left; i <= right; i++)
                    temp.push_back(i);
                result.push_back(temp); //push符合要求的连续正数序列,不止一组
                left++; //继续下个序列的寻找
            }
        }
        return result;
    }
};

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值