1. 题目原址
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
2. 题目描述
3. 题目大意
给定一个升序数组,和一个目标值,找到数组中的两个数的和等于目标值,然后返回这两个数的下标
4. 解题思路
签到题。
使用两个指针。
5. AC代码
class Solution {
public int[] twoSum(int[] numbers, int target) {
int s=0,h=numbers.length-1;
while(s<h)
{
if(numbers[s]+numbers[h]>target)
{
h--;
}else if(numbers[s]+numbers[h]<target)
{
s++;
}else{
return new int[]{s+1,h+1};
}
}
return null;
}
}
6. 相似题型
【1】 1. Two Sum 题目原址:https://leetcode.com/problems/two-sum/
【2】 15. 3Sum 题目原址: https://leetcode.com/problems/3sum/
【3】 18. 4Sum 题目原址: https://leetcode.com/problems/4sum/
【4】167. Two Sum II - Input array is sorted 题目原址: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
【5】 560. Subarray Sum Equals K 题目原址:https://leetcode.com/problems/subarray-sum-equals-k/
【6】 653. Two Sum IV - Input is a BST 题目原址:https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
【7】 16. 3Sum Closest 题目原址: https://leetcode.com/problems/3sum-closest/