Insist on doing small things, then witness the magic
123456789101112131415
class Solution { public int[] twoSum(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left < right) { if (nums[left] + nums[right] == target) { return new int[] { left + 1, right + 1 }; } else if (nums[left] + nums[right] < target) { left += 1; } else { right -= 1; } } return new int[0]; }}
two pointers. 没什么好说的.
时间复杂度: O(n)空间复杂度: O(1)
Calm Patient Persistent