leetcode--Two Sum II - Input array is sorted
2017-08-08 15:02
357 查看
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
题意:给你一个升序排列的数组和一个target,在数组中找出两个和为target的整数。
返回这两个整数的index,并且要求index1小于index2
注意,index是基于1开始的,也就是说数组的第一个index是1,而不是0
分类:数组
解法1:这个题目比Two
Sum http://blog.csdn.net/crazy__chen/article/details/45441671
还要简单,因为数组已经排好序了,用两个指针分别从头尾开始找就可以了。
如果头尾和大于target,说明尾太大,小于target说明头太小。
根据大小移动指针即可。
[java] view
plain copy
public class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers==null || numbers.length < 1) return null;
int i=0, j=numbers.length-1;
while(i<j) {
int x = numbers[i] + numbers[j];
if(x<target) {
++i;
} else if(x>target) {
–j;
} else {
return new int[]{i+1, j+1};
}
}
return null;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/47338641
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
题意:给你一个升序排列的数组和一个target,在数组中找出两个和为target的整数。
返回这两个整数的index,并且要求index1小于index2
注意,index是基于1开始的,也就是说数组的第一个index是1,而不是0
分类:数组
解法1:这个题目比Two
Sum http://blog.csdn.net/crazy__chen/article/details/45441671
还要简单,因为数组已经排好序了,用两个指针分别从头尾开始找就可以了。
如果头尾和大于target,说明尾太大,小于target说明头太小。
根据大小移动指针即可。
[java] view
plain copy
public class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers==null || numbers.length < 1) return null;
int i=0, j=numbers.length-1;
while(i<j) {
int x = numbers[i] + numbers[j];
if(x<target) {
++i;
} else if(x>target) {
–j;
} else {
return new int[]{i+1, j+1};
}
}
return null;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/47338641
相关文章推荐
- leetcode 167--- Two Sum II - Input array is sorted
- Java [Leetcode 167]Two Sum II - Input array is sorted
- Leetcode-Two Sum II - Input array is sorted
- leetcode167~Two Sum II - Input array is sorted
- [leetcode 167] Two Sum II - Input array is sorted
- [C语言][LeetCode][167]Two Sum II - Input array is sorted
- [Leetcode]Two Sum II - Input array is sorted
- [LeetCode]Two Sum II - Input array is sorted
- Leetcode Two Sum II - Input array is sorted
- LeetCode Two Sum II - Input array is sorted(Two pointer问题)
- LeetCode:Two Sum II - Input array is sorted
- leetcode :Binary Search:Two Sum II - Input array is sorted(069)
- leetcode -- Two Sum II - Input array is sorted
- LeetCode[167]Two Sum II - Input array is sorted
- leetcode2 Two Sum II – Input array is sorted
- LeetCode-167:Two Sum II - Input array is sorted (固定和的元素的索引 II)
- Leetcode之Two Sum II - Input array is sorted 问题
- LeetCode 167 Two Sum II - Input array is sorted (两点法)
- [Leetcode] Two Sum II - Input array is sorted
- LeetCode 167:Two Sum II - Input array is sorted