LeetCode_Study_Plan/Algorithm

167. Two Sum II - Input Array Is Sorted java

개발하는루루 2022. 12. 20. 23:30

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

 

Two Sum II - Input Array Is Sorted - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] ans = new int[2];
        for (int i = 0; i < numbers.length; i++) {
            if (i > 0 && numbers[i-1] == numbers[i]) continue;
            for (int j = i + 1; j < numbers.length; j++) {
                int sum = numbers[i] + numbers[j];
                if (sum == target) {
                    ans[0] = i + 1;
                    ans[1] = j + 1;
                    break;
                }
            }
        }

        return ans;
    }
}