167. Two Sum II - Input array is sorted

问题描述

解决方案


class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int index1=0,index2=numbers.size()-1;
        while(index1<index2)
        {
            if((numbers[index1]+numbers[index2])==target) break;
            else if((numbers[index1]+numbers[index2])>target) --index2;
            else
                ++index1;
        }
        return vector<int> {index1+1,index2+1};
    }
};
posted @ 2016-08-23 20:35  弦断  阅读(103)  评论(0编辑  收藏  举报