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 ...
随机推荐
- ResourceManager高可用配置
ResourceManager高可用配置 1. yarn-site.xml配置 <property> <name>yarn.resourcemanager.cluster-id ...
- Sqlserver 安装
安装环境: SqlServer版本:Sql Server 2008 (安装包您应该已有准备) =============以下开始安装,多图,基本软件操作不做太多说明,注意查看图片=========== ...
- JavaScript生成GUID的多种算法小结
全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...
- 使用struts+spring+hibernate组装web应用
这篇文章将讨论怎样组合几个着名的框架去做到松耦合的目的,怎样建立你的构架,怎样让你的各个应用层保持一致.富于挑战的是:组合这些框架使得每一层都以一种松耦合的方式彼此沟通,而与底层的技术无关.这篇文章将 ...
- string[] 和 arraylist互转及问题解决
1,String 数组转成 list<String> String[] s={"1","2","3","5" ...
- asp.net 备份和恢复数据库
观看了Insus的视频写下来的,代码可能有点冗长,如有好的想法的,可以多交流. 前台: <form id="form1" runat="server"&g ...
- STL容器与配接器
STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector 行为类似于数组,但可以根据要求 ...
- sgu 105 Div 3
一个数能整除3当且仅当各位数之和能整除3. 有了这个规律就好办了, 但是呢,仔细一看, n太大了, 都到 2^31 了.所以简单的模拟肯定不行. 这种貌似像数论的题,一时找不到好办法,就打表! 打表出 ...
- 九度OJ 1207 质因数的个数
题目地址:http://ac.jobdu.com/problem.php?pid=1207 题目描述: 求正整数N(N>1)的质因数的个数. 相同的质因数需要重复计算.如120=2*2*2*3* ...
- linux下查看端口的连接数
linux下,可以通过natstat命令来查看端口的连接状况,比如连接数 例如,查看9090端口的连接状况: 查看某个端口的连接数netstat -nat | grep -iw "9090& ...