首页 > 其他 > 详细

167.Two Sum II - Input array is sorted

时间:2019-04-12 10:11:26      阅读:121      评论:0      收藏:0      [点我收藏+]
// O(n)
class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int l = 0, r = numbers.size() - 1;
        while (l < r) {
            int sum = numbers[l] + numbers[r];
            if (sum == target) return {l + 1, r + 1};
            else if (sum < target) ++l;
            else --r;
        }
        return {};
    }
};

167.Two Sum II - Input array is sorted

原文:https://www.cnblogs.com/smallredness/p/10694107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!