【双指针_和为 s 的两个数_C++】

和为s的两个数字

cpp 复制代码
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n = nums.size();
        int left = 0;
        int right = n-1;
        while(left<right){
            if(nums[left]+nums[right]>target)   right--;
            else if(nums[left]+nums[right]<target)  left++;
            else  return  {nums[left],nums[right]};
        }
        return {-1,-1};
    }
};

注意:

return {-1,-1};这句代码是为了照顾力扣的编译器的

相关推荐
初见无风3 分钟前
3.4 Boost库intrusive_ptr智能指针的使用
开发语言·boost
mit6.8245 分钟前
[HDiffPatch] 补丁算法 | `patch_decompress_with_cache` | `getStreamClip` | RLE游程编码
c++·算法
程序猿20236 分钟前
Python每日一练---第六天:罗马数字转整数
开发语言·python·算法
装不满的克莱因瓶34 分钟前
【Java架构师】各个微服务之间有哪些调用方式?
java·开发语言·微服务·架构·dubbo·restful·springcloud
杨筱毅41 分钟前
【穿越Effective C++】条款13:以对象管理资源——RAII原则的基石
开发语言·c++·effective c++
煤球王子1 小时前
学而时习之:C++中的引用
c++
Zz_waiting.1 小时前
统一服务入口-Gateway
java·开发语言·gateway
L_09071 小时前
【Algorithm】Day-11
c++·算法·leetcode
四维碎片2 小时前
【Qt】大数据量表格刷新优化--只刷新可见区域
开发语言·qt
薛慕昭2 小时前
C语言核心技术深度解析:从内存管理到算法实现
c语言·开发语言·算法