题目:

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

链接: http://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

题解:

排序好的数组求two sum。用头尾两个指针对着夹逼一下就可以了。

Time Complexity - O(n), Space Complexity - O(1)。

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

二刷:

和一刷一样,双指针夹逼

Java:

public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = {-1, -1};
if (numbers == null || numbers.length == 0) {
return res;
}
int lo = 0, hi = numbers.length - 1;
while (lo <= hi) {
int sum = numbers[lo] + numbers[hi];
if (sum < target) {
lo++;
} else if (sum > target) {
hi--;
} else {
res[0] = lo + 1;
res[1] = hi + 1;
return res;
}
}
return res;
}
}

三刷:

Java:

public class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) return nums;
int lo = 0, hi = nums.length - 1;
while (lo < hi) {
int sum = nums[lo] + nums[hi];
if (sum == target) return new int[] {lo + 1, hi + 1};
else if (sum < target) lo++;
else if (sum > target) hi--;
}
return new int[] {-1, -1};
}
}

Reference:

167. Two Sum II - Input array is sorted的更多相关文章

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

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

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

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

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

  5. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

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

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

  8. 167. Two Sum II - Input array is sorted (Array)

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

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

随机推荐

  1. GWT环境搭建--eclipse

    上面下来需求,需要用到GWT,以前没接触过,搭个开发环境研究研究 安装软件我放在百度云盘里了(其他版本自己找,我的版本 eclipse4.4 luna  gwt2.7) 链接:http://pan.b ...

  2. .net 文件操作

    一.DotNet文件目录常用操作: DiveInfo:提供了对逻辑磁盘的基本信息访问的途径.(只能查看信息,不能做任何修改.) System.Environment:用来枚举驱动器.(不能获取驱动器的 ...

  3. C/C++雷区之内存管理

    C++雷区之内存管理 伟大的Bill Gates 曾经失言: 640K ought to be enough for everybody — Bill Gates 1981 程序员们经常编写内存管理程 ...

  4. ### CUDA

    CUDA Learning. #@author: gr #@date: 2014-04-06 #@email: forgerui@gmail.com 1. Introduction CPU和GPU的区 ...

  5. html19-----视频,音乐的插入

    视频格式 MP4 格式是一种新的即将普及的因特网视频格式.HTML5 .Flash 播放器以及优酷等视频网站均支持它. 格式 文件 描述 AVI .avi AVI (Audio Video Inter ...

  6. 规则引擎ILog和CKRule的对比

    IBM™ WebSphere™ ILOG是业界最有影响力的业务规则商业软件,它提供了最好的业务规则管理系统,在Java领域更是有广泛的成功案例.网上关于ILOG的技术资料非常多,大家都比较了解ILOG ...

  7. [leetcode] 403. Frog Jump

    https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...

  8. Percona-Server-5.5.15源码安装

    [root@localhost rpm]# ll total 19148 -rw-r--r-- 1 root root   562628 Jan 18  2007 bison-2.3-2.1.x86_ ...

  9. sharepoint 2013 suitbar

    参考链接:http://academy.bindtuning.com/customize-sharepoint-2013-and-office-365-suite-bar/

  10. css3 回到顶部书写

    回到顶部 JS 代码  backTop = function(){  if(!document.querySelector("#backTop")){return;}        ...