# Array167Two Sum II - Input array is sorted

2016-11-29

One failed case: [0,0,3,4] target: 0 Logic Loophole I initially wrote this line at the beginning of the loop if (numbers[j] >= target) j--; Then I find I didn’t consider the 0 case so changed into > rather than >= Code Optimization

### 思路1: Use two pointers

One failed case: [0,0,3,4] target: 0 Logic Loophole I initially wrote this line at the beginning of the loop if (numbers[j] >= target) j--; Then I find I didn&rsquo;t consider the 0 case. so changed into > rather than >= Code Optimization My first-time solution twoSumByTwoPointers1() is not concise. see the twoSumByTwoPointers2() makes some thinking
Decrements of j are the same condition before and after locatingnumbers[j] ~~ target. If not consider time complexity, it can be merged The i and j won&rsquo;t miss any possibilities when i < j because i++ and j&ndash; are only solutions in respect of two potential conditions sum < targetand sum > target

### 思路2: BinarySearch + two pointers

```* 用binarySearch找到end的初始位置, 然后用two pointers
* For the binary search, the start have to change, to better locate the mid.
```

### 思路3: 纯BinarySearch

```fromIndex - the index of the first element (inclusive) to be searched
toIndex - the index of the last element (exclusive) to be searched```

[0, 0, 3, 4] 0