[LeetCode]167. Two Sum II - Input array is sorted(输入两数和 II - 输出排序数组这两数位置)

Onnxruntime之YOLOP部署过程记录 魔改YoloP部署过程记录 阅读详情

167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题目大意:
给定一个升序的数组和一个正整数target,找出数组中哪两个数加起来得到target,输出这两个数的位置,注意输出的前一个数一定要比后一个数小。

代码如下:

#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int> Array(2);
        int left = 0;
        int right = numbers.size() - 1;
        while(left < right){
            if(numbers[left]+numbers[right] == target){
                Array[0] = left+1;
                Array[1] = right+1;
                return Array;
            }
            else if(numbers[left]+numbers[right] > target)
                right--;
            else
                left++;
        }
        return Array;
    }
};
int main()
{
    Solution a;
    int A[4] = {2, 7, 11, 15};
    vector<int> numbers1(A, A+4);
    vector<int> Array = a.twoSum(numbers1, 9);
    cout << Array[0] << ", " << Array[1] << endl;

    int B[2] = {5, 5};
    vector<int> numbers2(B, B+2);
    Array = a.twoSum(numbers2, 10);
    cout << Array[0] << ", " << Array[1] << endl;

    int C[3] = {1,5,9};
    vector<int> numbers3(C, C+3);
    Array = a.twoSum(numbers3, 14);
    cout << Array[0] << ", " << Array[1] << endl;
    cout << "Hello world!" << endl;
    return 0;
}

注意:

  1. 看Discuss有人直接 return { l + 1 , r + 1}; 貌似节省了空间
  2. 见到有人这么说,两个int的加和可能溢出int,将tmp和target提升为long long int再进行更好。
  3. 最初想法是二分查找,感觉太复杂就没写,可以参考下面的链接http://m.blog.csdn.net/article/details?id=53133707(该博主二分法描述部分是错误的,但是代码对了)
Qt入门教程【数据类型篇】数值类型 Qt中的数值类型 阅读详情

相关推荐

游戏资源解包神器NSCB实战:手把手教你提取Switch游戏视频(nsm转MP4)

本文详细介绍了如何使用NSCB工具链从Switch游戏中提取nsm视频和hca音频资源,并将其转换为可编辑的MP4和MP3格式。通过NSC Builder、CriPakTools等工具的分步操作指南,帮助内容创作者高效获取游戏资源,同时提供音视频同步合成、批量处理等高级技巧,为创意素材制作提供完整解决方案。

weixin_29197835的博客 920

Leetcode-167. Two Sum II - Input array is sorted c语言

1、题目:点击打开链接 2、下面代码是暴力破解,超时无法通过: /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* numbers, int numbersSize,

0_o_c 1483

Arduino四足开源机器猫-Opencat(导航贴)

本内容由我和小伙伴共同编写,未经授权,谢绝转载! 1.项目介绍 2.组装调试教程 (一)Opencat-F (1)资料地址(包含程序代码&&3D打印图纸): 链接:https://pan.baidu.com/s/1AcVA5jSiOSzRKqEQAomrTA 提取码:szfx (2) (二)Opencat-B (1)组装调试 (2)组装调试注意事项 (3)添加新技能 3.编程 ...

qqliuzhitong的博客 4180

LeetCode oj 167. Two Sum II - Input array is sorted (双指针)

167. Two Sum II - Input array is sorted   Question Editorial Solution  My Submissions Total Accepted: 29576Total Submissions: 61336Difficulty: Medium Given an array of in

Tristeza的博客 1158

zemax显微镜设计

光学显微镜(Optical Microscope,简写OM)是利用光学原理,把人眼所不能分辨的微小物体放大成像,以供人们提取微细结构信息的。我这里是插入进来之后才反转,如果之前已经在目镜的单独文件夹反转过了就不用了。将目镜的焦距设定为25mm,通过缩放,然后将镜头调转过来。镜头数据中的曲率半径、物距像距都设置为变量。将镜头的焦距整体变为16.25mm。将孔径类型设置为按照光阑尺寸浮动。打开物镜的文件,选择插入镜头。由英国物理学家虎克制作。2、像空间孔径0.25。3、像面大小-0.9。

fendoudecheng的博客 8554

3D Slicer中安装MedSAM插件

大家应该都读过这篇论文:《Segment Anything in Medical Images》这个插件便是该论文团队推出的在3D Slicer上使用的插件,可以分割医学图像的任何感兴趣区域。

m0_73719342的博客 2346

LeetCode167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers ...

AIpush的博客 474

Python 爬虫实战:爬取今日头条文章与阅读量数据

本文详细介绍了基于Python爬取今日头条文章与阅读量数据的完整方案。通过分析今日头条API接口,采用Requests+正则表达式技术实现数据抓取,重点解决加密参数构造、请求头模拟、反爬规避等核心问题。文章包含文章列表获取、详情数据提取、互动数据采集等完整流程,并提供数据存储与基础分析方法。同时强调合规使用爬虫技术,控制爬取频率,避免干扰平台运营。该方案可为内容传播分析、用户兴趣研究提供数据支持,但需注意接口参数更新和反爬机制变化,适时调整代码实现。

2503_91057718的博客 2524

LeetCode 167. Two Sum II - Input array is sorted 题解(C++)

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. - The function twoSum should return indices of the two numbers such th

prayyojay的博客 1460

Sigrity Power SI Model Extraction模式如何提取电源网络的S参数和阻抗操作指导(一)

34. 默认格式是BNP format格式,会被Sigrity识别的S参数文件,在仿真完成后,默认会在文件夹中生成bnp和ckt(MCP)文件,无需这步操作。由于port设置的ref Z是50ohm,实际上电源网络的阻抗是没有这么高的,所以S参数回损曲线是基本上全反射的一个状态。Sigrity PowerSI是频域电磁场仿真工具,以下图为例介绍如果用它观测电源的网络的S参数以及阻抗的频域曲线.42. Spice格式的加载进来图标是橙色的,BNP格式的图标是灰色的。

weixin_54787054的博客 1294

IGCT 测试原理概述

IGCT(Integrated Gate-Commutated Thyristor,集成门极换流晶闸管)是一种高功率半导体器件,基于门极可关断晶闸管(GTO)的原理,但通过集成门极驱动单元和优化结构(如硬驱动技术),实现了更低的导通损耗、更快的开关速度和更高的可靠性。它适用于中高压大功率应用,如高压直流输电(HVDC)、中压变频器和模块化多电平变换器(MMC)。IGCT 的结构为四层 p-n-p-n 器件,门极电流控制导通和关断,阳极电流在关断时快速换流至门极,通常在 1 μs 内完成。

专注于前沿技术的探索与实践,热衷于从代码逻辑、架构设计到行业应用等多维度深挖技术本质。期待在技术之路上与你共同成长~ 252

167. Two Sum II - Input array is sorted [medium] (Python)

题目链接https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/题目原文 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific targ

Coder_Orz的博客 5615

Joule 终于会说 ABAP 了:从 ADT 提效到 ABAP AI SDK + ISLM 自建企业级 AI 场景的落地路线

生成结构化摘要(故障现象、初步原因、处理动作、备件消耗)自动给出建议分类(电气、机械、软件、网络)把摘要回写到工单抬头,方便后续统计与知识沉淀如果你用 CAP 来做这类服务,通常会在 Node.js 层封装一个对 AI Hub 的调用,再通过 OData 或 Event 把结果写回 SAP。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 353

[LeetCode] 167. Two Sum II - Input array is sorted

原题链接: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 1. 题目描述 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific...

Ber03的博客 264

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

167. Two Sum II - Input array is sorted

文章目录167. Two Sum II - Input array is sortedDescriptionSolutionsSubmissionsSummary 167. Two Sum II - Input array is sorted https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ Description So...

La La Land 166

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode日练笔记11 #350 #167 Intersection of Two Arrays II & Two Sum II - Input array is sorted

#350 Intersection of Two Arrays II Given two integer arraysnums1andnums2, returnan array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result inany order. 解题思路: 还是用回之...

Bittersweet_t的博客 1744

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode 167: Two Sum II - Input array is sorted

Two Sum II - Input array is sorted Total Accepted: 441 Total Submissions: 1017 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a

西施豆腐渣 9969

【LeetCode】167. Two Sum II - Input array is sorted

我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的读书笔记!期待您的关注,欢迎一起学习交流进步! 知乎主页:https://www.zhihu.com/people/MicrostrongAI/activities Github:https://github.com/Microstrong0305 个人博客:https://blo.

Microstrong 697

lc167. Two Sum II - Input array is sorted

167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum sh...

weixin_34345560的博客 198

【LeetCode最详尽解答】167-两数之和 II-输入有序数组 Two-Sum-II-Input-Array-Is-Sorted

欢迎收藏Star我的Machine Learning Blog:https://github.com/purepisces/Wenqing-Machine_Learning_Blog。如果收藏star, 有问题可以随时与我交流, 谢谢大家!这是一个典型的双指针问题。输入:, 输出:解释: 和 的和是 。因此,, 。我们返回 。我们将左指针初始化在数组的起始位置,将右指针初始化在数组的末尾。while 循环条件是 。如果 的和小于目标值,我们将左指针右移,因为数组是按升序排序的。如果和大于目标值,我们将右

weixin_53765658的博客 1190

两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)java_leetcode167

两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)java_leetcode167 题干 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。 说明: 返回的下标值(index1 和 index2)不是从零开始的。...

下雨了_简的博客 342

LeetCode题解(Java实现)——167.Two Sum II - Input array is sorted(两数之和II-输入数组有序)

文章目录Leetcode:167. Two Sum II - Input array is sorted Leetcode:167. Two Sum II - Input array is sorted 题目 &amp;nbsp;&amp;nbsp;Leetcode:167. Two Sum II - Input array is sorted 题目描述 &amp;nbsp;&amp;nbsp;给定一个已经从小到大排好序的整型...

樱木天亥 594

【LeetCode 面试经典150题】167. Two Sum II - Input Array Is Sorted 两数之和II - 输入有序数组

Given a 1-indexed array of integers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be and where 1 <= < <= .Return the indices of the two numbers, and , added b

qq_43513394的博客 410

leetcode 167. Two Sum II - Input Array Is Sorted 两数之和 II - 输入有序数组

因为是已排序数组,使用双指针逆向遍历解决

ln_ydc的专栏 615

LeetCode: 167. Two Sum II - Input array is sorted

LeetCode: 167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The

qha0731的专栏 313

leetcode python3 简单题167. Two Sum II - Input array is sorted

1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific t...

从此做一个俗人 247
上一篇: [LeetCode]283. Move Zeroes(把0移到数组后面)
下一篇: [LeetCode]530. Minimum Absolute Difference in BST(BST的最小绝对差)
whl_program
博客等级 码龄11年 101粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值