로딩
요청 처리 중입니다...

JAVA_LeetCode 167_Two Sum II - Input Array Is Sorted

 JAVA_LeetCode 167_Two Sum II - Input Array Is Sorted

JAVA_LeetCode 167_Two Sum II - Input Array Is Sorted 풀이 class Solution { public int[] twoSum(int[] numbers, int target) { // 두 포인터의 합계와 target 값을 비교하여 위치를 조정한다. int left = 0, right = numbers.length - 1, sum = 0; while(left < right) { sum = numbers[left] + numbers[right]; // 배열의 index는 0부터 시작하므로 1을 더해줌 // 합이 작으면 왼쪽 포인터를 오른쪽으로 이동 // 합이 크면 오른쪽 포인터를 왼쪽으로 이동 if(sum == target) return new int[]{left + 1, right + 1}; else if(sum < target) left++; else right--; } // 문제 조건상 답이 항상 존재하므로 도달하지 않음 return new ...