my java AC solution easy to understand!


  • 0
    J
    public class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int low = 0;
            int high = numbers.length - 1;
            
            while(low < high){
                if(numbers[low] + numbers[high] == target)
                    break;
                else if(numbers[low] + numbers[high] < target)
                    ++low;
                else
                    --high;
            }
            
            int[] res = new int[2];
            res[0] = low + 1;
            res[1] = high + 1;
            return res;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.