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.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

第一题就是两数相加的题,其实可以用同一种解法。

这算法很基础,也没什么特殊的点。

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

✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java的更多相关文章

  1. LeetCode 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. 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 ...

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

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

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

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

  6. (双指针 二分) leetcode 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 ...

  7. Java [Leetcode 167]Two Sum II - Input array is sorted

    题目描述: Given an array of integers that is already sorted in ascending order, find two numbers such th ...

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

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

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

随机推荐

  1. Js获取下拉框选定项的值和文本

    Js获取下拉框的值和文本网上提供了2种方法:但有些人很不负责任,他们根本没考虑到浏览器之间的差异导致的错误,导致很多新手琢磨了半天找不出错误! 下面我总结下Firefox和IE下获取下拉框选定项的值和 ...

  2. ThinkPHP 3.2 模板中的Angularjs 的变量{{$first}} 无法被解析

    ThinkPHP 3.2 模板中的Angularjs 的变量"{{$first}}" 无法被解析, 模板解析冲突,例如在angularjs 的变量"{{$first}}& ...

  3. iOS MD5加密字符串

    参考:http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c 在线测试MD5:http://www.cmd5.c ...

  4. SQL基础--完整性约束

    完整性约束是保证用户所做的修改不会破坏数据的一致性,是保护数据正确性和相容性的一种手段. 常见的5种约束: NOT NULL           非空约束C     指定的列不允许为空值 UNIQUE ...

  5. 打完补丁后测试db_link对SCN的影响

    环境:11.2.0.4.0 升 11.2.0.4.8 后测试 背景:oracle 的db_link会导致实例间SCN同步,SCN增长速度过快则会产生错误: 方案:oracle官方推荐升级版本,但升级之 ...

  6. 【matlab】膨胀

    clear all;close all; BW=zeros(9,10); BW(4:6,4:7) =1 imshow(BW) hold on SE=strel('square',3) BW2=imdi ...

  7. hdu 5876 ACM/ICPC Dalian Online 1009 Sparse Graph

    题目链接 分析:这叫补图上的BFS,萌新第一次遇到= =.方法很简单,看了别人的代码后,自己也学会了.方法就是开两个集合,一个A表示在下一次bfs中能够到达的点,另一个B就是下一次bfs中到不了的点. ...

  8. GDI+一般性错误(A generic error occurred in GDI+)

    1.GDI+的前世今生 GDI+全称图形设备接口,Graphics Device Interface (GDI) ,他的爸爸叫做GDI, 用C写的.Windows XP出来以后用C++重新写了一下,变 ...

  9. 弄清UTF8和Unicode

    长期以来,一直对字符串编码认识比较粗略,认为支持"特殊字符"编码就是Unicode.当然,.NET平台上很少需要考虑这类问题,但搞清一些基本概念还是很有好处的. Unicode这个 ...

  10. [html5]placeholder默认颜色

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #f00; } ::-moz-placeholder { /* Mozilla Fir ...