剑指OFFER----面试题57. 和为s的两个数字

链接:https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/

代码

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n = nums.size() - 1;
        int i, j, tmp;
        while (nums[n] >= target) n--;

        i = 0, j = n;
        while (i < j) {
            tmp = nums[i] + nums[j];
            if (tmp == target) {
                return vector<int>{nums[i], nums[j]};
            } else if (tmp > target) j--;
            else i++;
        }
        return vector<int>();
    }
};
posted @ 2020-03-11 14:43  景云ⁿ  阅读(109)  评论(0编辑  收藏  举报