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.
 
思路:
two pointers法:i,j 分别指向sorted array的头和尾,如果numbers[i] + numbers[j] < target, 左指针后移,否则有指针左移。当i = j时,则没有找到和为target对应的index。则返回空。
 
注意:
j = numbers.length - 1; 不要再忘了-1 才是数组最后一位的index!!
 
代码:
    public int[] twoSum(int[] numbers, int target) {

        int i = 0, j = numbers.length - 1;

        while (i != j) {
int sum = numbers[i] + numbers[j];
if (sum == target) {
return new int[]{i+1, j+1};
}
else if (sum > target) {
j--;
}
else {
i++;
}
}
return new int[]{};
}

Leetcode167-Two Sum II Input array is sorted-Easy的更多相关文章

  1. LeetCode167. Two Sum II - Input array is sorted(双指针)

    题意:对于一个有序数组,输出和为target的两个元素的下标.题目保证仅有唯一解. 分析: 法一:二分.枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解. class Solut ...

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

  3. 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], ...

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

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

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

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

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

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

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

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

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

  10. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

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

随机推荐

  1. 蒙特卡洛(Monte Carlo)法求定积分

    https://blog.csdn.net/baimafujinji/article/details/53869358

  2. Java Socket入门

    Java Socket底层采用TCP/IP协议通信,通信细节被封装,我们仅仅需要指定IP.端口,便能轻易地创建TCP或UDP连接,进行网络通信.数据的读写,可以使用我们熟悉的stream进行操作. T ...

  3. 设计模式之Prototype(原型)(转)

    定义: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传 ...

  4. 算法提高 P0101

    一个水分子的质量是3.0*10-23克,一夸脱水的质量是950克.写一个程序输入水的夸脱数n(0 <= n <= 1e10),然后输出水分子的总数.输入 109.43输出 3.465283 ...

  5. UVALive 3295 Counting Triangles

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. python 关键字yield解析

    python 关键字yield解析 yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator.y ...

  7. js 迭代方法

    迭代方法 * every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true. * filter():对数组中的每一项运行给定函数,返回该函数会返回true 的项组成的 ...

  8. django后台管理-ModelAdmin对象

    Django最强大的部分之一是自动生成的管理后台界面. 它从你的模型中读取元数据,以提供一个快速的.以模型为中心的界面,信任的用户可以在这里管理你网站上的内容. 建议管理后台仅作为组织的一个内部管理工 ...

  9. oracle exadata一体机虚拟机

    14年参加partner培训的时候,拿了份oracle exadata一体机虚拟机,有兴趣的可以试试,不过比较大,压缩后10GB,解压后50GB,启动后直接可用,2RAC节点+1存储节点,环境最好内存 ...

  10. c++ kafka 客户端rdkafka报Receive failed: Disconnected问题原因以及解决方法

    %3|1538976114.812|FAIL|rdkafka#producer-1| [thrd:kafka-server:9092/bootstrap]: kafka-server:9092/0: ...