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.
java
public class Solution {
/*
* @param nums: an array of Integer
* @param target: target = nums[index1] + nums[index2]
* @return: [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] nums, int target) {
// write your code here
if (nums == null || nums.length == 0 || nums.length == 1) {
return new int[]{};
}
int left = 0;
int right = nums.length - 1;
while (left <= right) {
if (nums[left] + nums[right] > target) {
right--;
} else if (nums[left] + nums[right] < target) {
left++;
}else {
return new int[] {left + 1, right + 1};
}
}
return new int[]{-1, -1};
}
}
python
class Solution:
"""
@param: nums: an array of Integer
@param: target: target = nums[index1] + nums[index2]
@return: [index1 + 1, index2 + 1] (index1 < index2)
"""
def twoSum(self, nums, target):
# write your code here
if nums is None or len(nums) == 0 or len(nums) == 1:
return [-1, -1]
left, right = 0, len(nums) - 1
while left <= right:
value = nums[left] + nums[right]
if value > target:
right -= 1
elif value < target:
left += 1
else:
return [left + 1, right + 1]