题目:

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. Xcode中,调试console窗口输出error: Couldn't materialize struct: the variable 'cell' has no location, it may have been optimized out的问题

    Xcode中调试代码时,常常需要使用console窗口查看变量的信息,比如使用了如下的命令来输出有关UITableView中一个UITableViewCell的信息, po cell 令人感到意外的是 ...

  2. ios 经典错误

    1 - [person test]:unrecognized selector sent to instance. 给penson对象发送一个不能识别的消息:test   2 set/get方法死循环 ...

  3. WIX: Hide installed program from the Add/Remove Programs window.

    Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set ...

  4. 九度OJ 1209 最小邮票数 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1209 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3 ...

  5. JS 版的pnp in_array($str,$arr)

    var a = Array(1,2,3,4,5); function in_array(search,array){ for(var i in array){ if(array[i]==search) ...

  6. form表单重置

    Jquery中重置表单的错误姿势 $('#yigeform').reset() 正确姿势 $('#yigeform')[0].reset()

  7. C#基础(三)—重载与覆盖

    所谓重载指的是同一个类中有两个或多个名字相同但是参数不同的方法.重载,必然发生在一个类中,函数名相同,参数类型或者顺序不同构成重载,与返回类型无关. override:过载也称重写是指子类对父类中虚函 ...

  8. jquery垂直公告滚动实现代码

    公告滚动想必大家都有见到过吧,实现方法也有很多,下面为大家介绍使用jquery实现垂直公告滚动,感兴趣的朋友不要错过 复制代码代码如下: <!DOCTYPE html PUBLIC " ...

  9. 安装saltstack

    1.安装master 安装epel源 # cd /usr/local/src/ # wget http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-rel ...

  10. Linux统计某文件夹下文件、文件夹的个数

    统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...