Two Sum II - Java Solution using Two Pointers


  • 0
    D
    public class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int left = 0;
            int right = numbers.length-1;
            int sum;
            
            while( left < right ) {
                sum = numbers[left] + numbers[right];
                if(  sum == target )
                    return new int[]{left+1, right+1};
                else if ( sum < target )    left++;
                else right--;
            }
            return null;
        }
    }
    

Log in to reply
 

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