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.

思路:

与1. Tow Sum类似,这题的输入是有序数组,限定了一定会有解,用双指针来做,定义左右两个指针,左指针指向第一个数,右指针指向最后一个数,然后用这两个数的和与Target比较,如果比Target小,左指针向右移一位,如果比Target大,右指针向左移一位。然后再进行比较,直到找到或者两个指针相遇为止。

注意:左右指针是从0到 len(numbers)-1, 输出结果是从1开始的index.

Time: O(n)  Space: O(1)

Java: wo,  0 ms, faster than 100.00% of Java online submissions

class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
int i = 0, j = numbers.length - 1;
while (i < j) {
if ((numbers[i] + numbers[j]) > target) {
j--;
} else if ((numbers[i] + numbers[j]) < target) {
i++;
} else {
res[0] = i + 1;
res[1] = j + 1;
break;
}
}
return res;
}
}  

Java: 1 ms, faster than 68.07% of Java online submissions

public class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers==null || numbers.length < 1) return null;
int i=0, j=numbers.length-1; while(i<j) {
int x = numbers[i] + numbers[j];
if(x<target) {
++i;
} else if(x>target) {
--j;
} else {
return new int[]{i+1, j+1};
}
}
return null;
}
}   

Python:

class Solution:
def twoSum(self, nums, target):
start, end = 0, len(nums) - 1 while start != end:
sum = nums[start] + nums[end]
if sum > target:
end -= 1
elif sum < target:
start += 1
else:
return [start + 1, end + 1]  

C++:

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int l = 0, r = numbers.size() - 1;
while (l < r) {
int sum = numbers[l] + numbers[r];
if (sum == target) return {l + 1, r + 1};
else if (sum < target) ++l;
else --r;
}
return {};
}
};

  

相似题目:

[LeetCode] 1. Two Sum 两数和

[LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

[LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

[LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组的更多相关文章

  1. 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...

  2. [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  3. [LeetCode] 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 ...

  4. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  5. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

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

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

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

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

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

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

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

随机推荐

  1. Python爬取mn52网站美女图片以及图片防盗链的解决方法

    防盗链原理 http标准协议中有专门的字段记录referer 一来可以追溯上一个入站地址是什么 二来对于资源文件,可以跟踪到包含显示他的网页地址是什么 因此所有防盗链方法都是基于这个Referer字段 ...

  2. 摘:J2EE开发环境搭建(1)——安装JDK、Tomcat、Eclipse

    J2EE开发环境搭建(1)——安装JDK.Tomcat.Eclipse 1:背景 进公司用SSH(Struts,spring和hibernate)开发已经有两个月了,但由于一 直要么只负责表示层的开发 ...

  3. clause

    clause 英 [klɔːz]  美 [klɔz]  口语练习 跟读 n. 条款:[计] 子句 specify 英 ['spesɪfaɪ]  美 ['spɛsɪfaɪ]  口语练习 跟读 vt. 指 ...

  4. janusgraph-控制台操作命令

    当顶点数量过多时(我的230w)删除太慢 就用下面的命令, 删除整个图库 graph.close() JanusGraphFactory.drop(graph) 查询所有的顶点属性 用traversa ...

  5. go 学习 (五):包管理

    一.设置环境变量 二.启用 go modules 功能 并设置代理 https://goproxy.io/zh/ 补充: GO111MODULE  有三个值:on.off.auto GO111MODU ...

  6. 2019.12.09 java循环(do……while)

    class Demo05{ public static void main(String[] args) { int sum=0; int i=1; do{ sum+=i; i++; }while(i ...

  7. WinDbg常用命令系列---内存数据显示和对应符号显示d*s(dds、dps、dqs)

    命令dds, dps,  dqs显示给定范围内的内存内容.假定该内存是符号表中的一系列地址.相应的符号也会显示出来. dds [Options] [Range] dqs [Options] [Rang ...

  8. .NET体系结构

    主要内容包括: C#与.NET的关系.公共语言运行库.中间语言.程序集..NET Framework类.名称空间.内层管理... C#与.NET的关系 C#是门高级编程语言,.NET(Framewor ...

  9. P1913 L国的战斗之伞兵

    题目链接 P1913 L国的战斗之伞兵 思路 从无风点倒着dfs,本来是道大水题,结果输入的时候第二层循环打错了!手残打成i++ 代码 #include<iostream> #includ ...

  10. P2052 [NOI2011]道路修建——树形结构(水题,大佬勿进)

    P2052 [NOI2011]道路修建 这个题其实在dfs里面就可以把事干完的,(我一开始还拿出来求了一把)…… 一条边的贡献就是儿子的大小和n-siz[v]乘上边权: #include<cma ...