# 167. Two Sum II - Input array is sorted

{% embed url="<https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/>" %}

Array 中找 pair，很显然的双指针。

{% tabs %}
{% tab title="Python" %}

```python
class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        l, r = 0, len(numbers)-1
        while l<r:
            cur = numbers[l] + numbers[r]
            if cur == target: return [l+1, r+1]
            if cur > target: r-=1
            elif cur < target: l+=1
```

{% endtab %}

{% tab title="Java" %}

```java
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int l=0, r=numbers.length-1;
        int sum;
        while (l<r) {
            sum = numbers[l] + numbers[r];
            if (sum == target) break;
            if (sum > target) {
                r--;
            } else {
                l++;
            }
        }
        int[] res = new int[2];
        res[0] = l+1;
        res[1] = r+1;
        return res;
    }
}
```

{% endtab %}
{% endtabs %}
