描述

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 and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

分析

用的是二分的思想。

考虑到输入的是递增的有序数组,且有且仅有一组输出结果,因此可以用两个变量,分别从数组起始处i和末尾处j开始,如果两个数相加等于target,就返回这两个下标;如果相加大于target,那么就减小j的值,如果相加小于target,那么就增加i的值,直到找到为止。

代码如下:

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
/*
Time exceeded code
int i = 0;
int len = 0;
vector<int>ret;
for(auto it = numbers.begin(); it != numbers.end(); ++it){
int another = target - *it;
auto index = find(it + 1,numbers.end(),another);
if(index != numbers.end()){
len = distance(it,index);
break;
}
++i;
}*/ int i = 0,j = numbers.size() - 1;
while(i < j){
if(numbers[i] + numbers[j] == target){
vector<int>ret = {i + 1,j + 1};
return ret;
}else if(numbers[i] + numbers[j] > target)
--j;
else
++i;
} }
};

leetcode解题报告(22):Two Sum II - Input array is sorted的更多相关文章

  1. [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  2. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  3. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  5. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  6. LeetCode_167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...

  7. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

  8. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  9. 167. Two Sum II - Input array is sorted@python

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  10. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

随机推荐

  1. Java 总结篇1

    初始Java 1.Java的特点: ① 跨平台(字节码文件可以在任何具有Java虚拟机的计算机或者电子设备上运行,Java虚拟机中的Java解释器负责将字节码文件解释成特定的机器码进行运行) ② 简单 ...

  2. 《Java Web开发实战》——Java工程师必备干货教材

    一年一度毕业季,又到了简历.offer漫天飞,失望与希望并存的时节.在IT行业,高校毕业生求职时,面临的第一道门槛就是技能与经验的考验,但学校往往更注重学生的理论知识,忽略了对学生实践能力的培养,因而 ...

  3. Python遗传和进化算法框架(一)Geatpy快速入门

    https://blog.csdn.net/qq_33353186/article/details/82014986 Geatpy是一个高性能的Python遗传算法库以及开放式进化算法框架,由华南理工 ...

  4. NRF52832 Mesh调试,使其同时支持串口打印和RTT打印

    查看开发环境里面,是否有这个文件,如果没有你的话,则添加文件. 然后要在sdk_config.h中添加使能 然后打开刚才添加的文件retarget.c,主意里面这些地方 这里它进行判断,要么使用RTT ...

  5. 我们为什么要用redis

    Redis的5要点: 1.为什么要选择Redis:介绍Redis的使用场景与使用Redis的原因: 2.Redis常用命令总结:包括时间复杂度总结与具体数据类型在Redis内部使用的数据结构: 3.R ...

  6. 【转载】为什么我的网站加www是打不开的呢

    在访问网站的过程中,我们发现有些网站访问不带www的主域名可以正常访问,反而访问加www的域名打不开,那为什么有的网站加www是打不开的呢?此情况很大可能是因为没有解析带www的域名记录或者主机Web ...

  7. JavaScript时钟效果

    在JavaScript中,有一个内置对象Date,它重要的一个作用就是实现了时间的时刻更新,通过代码来创造一个实实在在的时间表. 代码例子: <!DOCTYPE html> <htm ...

  8. How to : SAP PI Cache Refresh

    Requirement : Identify various tools/resources available to perform SAP PI Cache refresh . Please no ...

  9. markdown格式字串转html格式字串

    参考:https://www.jianshu.com/p/0eff6cba1b7f 操作: 1.安装python支持包 pip install mkdocs       #  含markdown包 或 ...

  10. lucene初探

    http://www.cnblogs.com/xing901022/p/3933675.html