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. [bzoj2124]等差子序列(hash+树状数组)

    我又来更博啦     2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Statu ...

  2. 简单说一下printf("%*s%s",xx,xx,xx);或printf("%*s\n",xx,xx);

    大家还记得这个例子吗 #include "public.h" int main() { ; printf("%4d\n",a); ; } 这个输出结果为:    ...

  3. linux 之SCP

    一.从本地到远程复制 1.复制文件 * 命令格式: 1.scp -P remote_port local_file remote_username@remote_ip:remote_folder 或者 ...

  4. Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\Program Files\Java\jre7

    Maven编译时两则信息 (Workspace以及default classpath container) 博客分类: Java   使用Maven一年有余,却总是被两则不起眼的编译信息困扰,终想查明 ...

  5. reg.test is not a function 报错

    正则中 比如 var reg = "/^[0-9]$/" 会报 reg.test is not a function 如果 var reg = /^[0-9]$/ 就不会有错 因为 ...

  6. MySQL限时解答

    MySQL在国内各个行业的使用率越来越高,使用场景也越多,相应的遇到的疑惑也越来越多.在遇到这些问题之后,目前已有的解决途径有 1.培训(这是从长计议的方式,不能解决燃眉之急) 2.BBS(目前BBS ...

  7. gitgub利用客户端实现简单的上传和同步

    新建项目 打开客户端(将项目拷贝到本地) 选择要clone到的文件夹 想该文件夹中,导入自己需要上传的代码 然后,在网站上登录自己的gitgub,就可以看到刚才上传的项目了╮(╯▽╰)╭

  8. MySQL学习笔记--数据类型

    一.数据类型(内容参考<SQL学习指南>)不完整 1.文本类型 文本类型 最大字节数 tinytext 255 text 65535 varchar 65536 mediumtext 16 ...

  9. BSD Apache GPL LGPL MIT

    当Adobe.Microsoft.Sun等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 最初来自:sinoprise.com/read.php?tid-662-page-e-fpa ...

  10. 创建JOB

    1.创建一张表g_test create table G_TEST ( ID NUMBER(12), C_DATE DATE ) 2.创建一个sequence create sequence G_SE ...