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

Posted clown9804

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指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>();
    }
};

以上是关于剑指OFFER----面试题57. 和为s的两个数字的主要内容,如果未能解决你的问题,请参考以下文章