描述

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. Python字符串图解

    >>> word = "Python" >>> word[:2]  # character from the beginning to posi ...

  2. EF Core 2.0 执行原始查询如何防止SQL注入

    using (var context = new EFCoreDbContext()) { var searchString = "Jeffcky Wang"; Formattab ...

  3. Istio最佳实践:在K8s上通过Istio服务网格进行灰度发布

    Istio是什么? Istio是Google继Kubernetes之后的又一开源力作,主要参与的公司包括Google,IBM,Lyft等公司.它提供了完整的非侵入式的微服务治理解决方案,包含微服务的管 ...

  4. SpringBoot学习(五)—— springboot快速整合Druid

    Druid连接池 简介 由阿里巴巴开源的druid连接池是目前综合实力最突出的数据库连接池,而且还提供了监控日志功能,能够分析SQL执行情况. 引入druid连接池 pom.xml中加入 <de ...

  5. idea for mac 快捷键整理

    ⌘O 查找类文件 ⌘⌥O 前往指定的变量 / 方法 ⌘⇧O 查找所有类型文件.打开文件.打开目录,打开目录需要在输入的内容前面或后面加一个反斜杠/ ⌘⌥← / ⌘⌥→ 退回 / 前进到上一个操作的地方 ...

  6. 路由基础(Routing)

    查看本机路由表: [root@controller02 ~]# cat /etc/iproute2/rt_tables # # reserved values # 255     local 254  ...

  7. vue使用技巧:Promise + async + await 解决组件间串行编程问题

    业务场景描述 大家都通过互联网投递过简历,比如在智联.58.猎聘等平台.投递心仪的职位前一般都需要前提创建一份简历,简历编辑界面常规的布局最上面是用户的个人基本信息,如姓名.性别.年龄.名族等,接着是 ...

  8. C++——static & const

    静态成员 由关键字static修饰说明的类成员,称为静态类成员(static class member).虽然使用static修饰说明,但与函数中的静态变量有明显差异.类的静态成员为其所有对象共享,不 ...

  9. python之tkinter入坑Pack()------(1)

    tkinter 的pack()可以设置的属性如下: pack_configure(self, cnf={}, **kw)Pack a widget in the parent widget. Use  ...

  10. 为0LTP选择RDMBS时,你都需要考虑哪些?

    我们经常需要为自己的OLTP(事务/运营)数据库选择适合的RDBMS(关系型数据库管理系统).虽然通过编写可移植的SQL可以暂时避免进行这样的选择,但迟早要做出这样的选择,至少需要进行这样的尝试(比如 ...