java solution with less comparsion


  • 0
    W
    public int[] twoSum(int[] numbers, int target) {
    	int temp = numbers.length;
    	int left = 0, right = temp - 1;
    	while (target < numbers[temp - 1]) {
    		right = temp - 1;
    		temp >>= 1;
    	}
    
    	while ((temp = numbers[left] + numbers[right]) != target) {
    		if (temp < target)
    			++left;
    		else
    			--right;
    	}
    	return new int[] { left + 1, right + 1 };
    }

Log in to reply
 

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