题解 | #两数之和#

两数之和

https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f

#include <asm-generic/errno.h>
class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int> vc=numbers;
        sort(vc.begin(),vc.end());
        int start=0;
        int end=numbers.size()-1;
        while(1)
        {
            if(vc[start]+vc[end]>target)
                end--;
            else if(vc[start]+vc[end]==target)
                break;
            else
                start++;
        }
        vector<int> res;
        for(int i=1;i<=numbers.size();++i)
        {
            if(vc[start]==numbers[i-1]||vc[end]==numbers[i-1])
            res.push_back(i);
        }
        return res;
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务