167. Two Sum II - Input Array Is Sorted
in Coding Interview on Medium, Array, Two Pointers
정렬된 배열의 투썸: Two Pointer
167. Two Sum II - Input Array Is Sorted
class Solution {
// 정렬된 배열의 투썸: Two Pointer
// T: O(n)
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
int first = 0, last = numbers.length - 1;
while (first < last) {
int sum = numbers[first] + numbers[last];
if (sum < target) {
first += 1;
} else if (sum > target) {
last -= 1;
} else {
result[0] = first + 1;
result[1] = last + 1;
break;
}
}
return result;
}
}