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 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的更多相关文章
- 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 ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 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 ...
- [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 ...
- (双指针 二分) 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 ...
- 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 ...
- [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 ...
随机推荐
- TortoiseSVN 安装中文语言包,SVN中文语言包
SVN中TortoiseSVN 是比较出门的一款SVN软件 TortoiseSVN 是Subversion 版本控制系统的一个免费开源客户端. 由于TortoiseSVN 默认是英文的:所以很多小伙伴 ...
- 在有大量数据时 少用In(数据会丢失) 用left join 代替
select @From, @To, EffectiveDate, GETDATE(), Rate from Config_Currency_ExchangeRate_Temp where Effec ...
- iOS Core Animation学习总结(2)--实现自定义图层
一. 创建图层继承于CALayer,并在子类实现drawInContext方法 @interface CTLayer : CALayer @end @implementation CTLayer -( ...
- dorado基本事件样例
var info = self.get("returnValue"); view.set("#labelProduct.text","产品:" ...
- OpenJudge/Poj 1753 Flip Game
1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...
- 解决rtl8723be无线网卡驱动频繁断网问题
买了新本子,用的是rtl8723be无线网卡,连WIFI时总是断网.Windows下好解决,Ubuntu下可就麻烦了,又是升级内核又是编译驱动的,折腾了一天,终于找到了解决办法: # echo &qu ...
- linux共享文件samba安装与java读取外部文件夹方法
测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...
- .NET高端职位招聘要求
系统架构师: 1.硕士及以上学历,博士有项目成果者优先: 2.五年以上工作经验,三年以上互联网经验,一年以上大型软件项目总体设计.分析.架构经验,有移动互联网或云计算虚拟化系统设计开发经验者优先: 3 ...
- PHP中进制之间的互相转换
常见的进制: 二进制 binary -----> bin 八进制 octal -----> oct 十进制 decimal -----> dec 十六进 ...
- hdu 5648 DZY Loves Math 组合数+深搜(子集法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...