技术栈

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

MegaDataFlowers2023-08-17 11:02

和为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};这句代码是为了照顾力扣的编译器的

上一篇:瓴羊发布All in One 产品,零售SaaS的尽头是DaaS?
下一篇:【编程二三事】ES究竟是个啥?
相关推荐
樱木Plus
2 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit
4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_
5 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星
5 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛
7 天前
delete又未完全delete
c++
端平入洛
8 天前
auto有时不auto
c++
郑州光合科技余经理
9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo123
9 天前
matlab画图工具
开发语言·matlab
dustcell.
9 天前
haproxy七层代理
java·开发语言·前端
norlan_jame
9 天前
C-PHY与D-PHY差异
c语言·开发语言
热门推荐
01GitHub 镜像站点02OpenClaw 使用和管理 MCP 完全指南03OpenClaw + 飞书(Feishu)环境搭建指南04本地部署 OpenClaw + DeepSeek-R1 完全指南05Window 10部署openclaw报错node.exe : npm error code 12806OpenClaw优化飞书API 额度已耗尽问题07Claude Code + GLM4.7 避坑指南:解决 Unable to connect to Anthropic services08OpenClaw 连接飞书完整指南:插件安装、配置与踩坑记录09OpenClaw 飞书机器人不回复消息?3 小时踩坑总结10小黑课堂计算机二级WPSoffice题库软件下载安装教程(2026年3月最新版)