题目:

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. jsf使用spring注入的bean

    jsf的后台bean中使用spring定义的service,需要使用@ManagedProperty,并且要具有该属性的getter/setter方法. package cn.catr.lm.idc. ...

  2. ArryList vs LinkedList

    references: http://www.javaperformancetuning.com/articles/randomaccess.shtml http://stackoverflow.co ...

  3. ios Trace xcode buile count

    前言: 1.记录xcode编辑次数很有必要,特别是在频繁发版本时和根据现有编译次数记录估算工期时间很有帮助 2.全部自动化处理,告别手动时代 正文: 1.新建工程或者现有工程里设置: 然后设置xcod ...

  4. GCD 多线程

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法.它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统.它是一个在线程池模式的基础上执行的 ...

  5. bzoj1016:[JSOI2008]最小生成树计数

    思路:模拟kruskal的过程,可以发现对于所有权值相同的边,有很多种选择的方案,而且权值不同的边并不会相互影响,因为先考虑权值较小的边,权值比当前权值大的边显然不在考虑范围之内,而权值比当前权值小的 ...

  6. 学习C++ Primer 的个人理解(十二)

    动态内存与智能指针 在C++中, 动态内存用 new来分配空间并返回一个指向该对象的指针 用delete来销毁. 由于手动的对动态内存进行操作容易出现问题.所以新的标准库提供了两种智能指针. 智能指针 ...

  7. I Take It All Back: Using Windows Installer (MSI) Rollback Actions

    Original Link: http://blogs.flexerasoftware.com/installtalk/2011/10/i-take-it-all-back-using-windows ...

  8. 【HeadFirst设计模式】9.迭代器与组合模式

    迭代器: 定义: 提供一种方法,顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示.(不让你知道我内部是如何聚合的) 把游走的任务放在迭代器上,而不是聚合上.这样简化了聚合的接口和实现,也让责任 ...

  9. debian 学习记录-3 -关于linux -1

    来源:<Debian标准教程>王旭 著 芬兰人Linus Trovalds 1991年1月2日····   2006年初发布内核2.6.15 使用Andrew Tanenbaum < ...

  10. MySQL之count(*)与count(id)效率比较(转)

    优化总结: 1.任何情况下SELECT COUNT(*) FROM tablename是最优选择: 2.尽量减少SELECT COUNT(*) FROM tablename WHERE COL = ’ ...