Leetcode | 167 Two Sum II - Input array is sorted

167 Two Sum II - Input array is sorted(两数之和 II - 输入有序数组)

题目】:链接 

【代码】:

class Solution 
{
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> rets;
        int high = nums.size() - 1;
        int low = 0;
        while(low != high)
        {
            int sum = nums[low] + nums[high];
            if( sum == target)
            {
                rets.push_back(low + 1);
                rets.push_back(high + 1);
                return rets;
            }
            else if(sum > target)
                --high;
            else
                ++low;
        }
        return rets;       
    }
};

猜你喜欢

转载自blog.csdn.net/isunbin/article/details/80990580