1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public int[] twoSum(int[] nums, int target) { int i = 0, j = nums.length - 1; while (i < j) { int sum = nums[i] + nums[j]; if (sum < target) { i++; } else if (sum > target) { j--; } else { return new int[]{nums[i], nums[j]}; } }
return new int[0]; } }
|