有序数组求和问题
一、学习要点:
1.c++中参数引用的应用,有一种c语言中传值的感觉;声明一个引用,不是新定义了一个变量,它只表示该引用名是目标变量名的一个别名,它本身不是一种数据类型,因此引用本身不占存储单元,系统也不给引用分配存储单元。
2.vector res(2,0)表示vector是一个二维向量,每个元素被初始化为0;用vector的时候注意包含库include;
二、代码实现:

#include<stdlib.h>
#include<stdio.h>
#include<vector>
class Solution{
public:vector<int> twoSum(vector<int> &numbers,int target){vector<int> res(2,2);int i=0;int j=numbers.size()-1;while(i<numbers.size()-1&&j>0){    int ans=numbers[i]+numbers[j];if(ans>target){j--;}else if(ans<target){i++;}else{res[0]=i+1;res[1]=j+1;return res;}}return res;}}
int main(){ vector<int> a={2,5,7,9};int target=9;vector<int> res(2,2);Solution ob;res=ob.twoSum(a,target);for(int i=0;i<2;i++){printf("%d",res[i]);}system("pause");return 0;
}

三、运行结果:

有序数组求和问题(Two Sum II - Input array is sorted)相关推荐

  1. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  2. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

  3. 167. Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    Title 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: ...

  4. LeetCode167 | Two Sum II - Input array is sorted (Easy)

    题目地址(167. 两数之和 II - 输入有序数组) https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ 题目描述 ...

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

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...

  6. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  7. 167. Two Sum II - Input array is sorted (C, C++, Python)

    本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++. 问题: Given an array of integers that is already sorted ...

  8. leetcode 167 Two Sum II - Input array is sorted

    给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从1开始.假设必定存在解. 有两种思路: 直接找: vector<int> twoSum(vector<int&g ...

  9. 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 tha ...

最新文章

  1. 数据结构与算法系列 目录
  2. oracle日期虚数0去掉,第 14 章 使用复数运算库
  3. matlab条形图颜色矩阵,matlab中的条形图开关颜色
  4. 【报告分享】2019年中国人工智能商业落地研究报告.pdf(附下载链接)
  5. CSS解决无空格太长的字母,数字不会自动换行的问题
  6. python属于汇编语言还是高级语言_计算机语言Python解释器
  7. 【Linux】自主实现my_sleep【转】
  8. 95%的码农都在用的编程神器,值得一看!
  9. 推荐收藏:50个最佳机器学习公共数据集
  10. 计算机考研数学英语政治考啥,2016年考研数学一英语一政治计算机考研大纲原文合集.docx...
  11. spyder python下载_Spyder Python软件-Spyder Python下载-最火手机站
  12. 微信公众号H5页面缓存问题(微信缓存了 index.html 入口)
  13. 访问html 403 iis,IIS 403 错误详细原因 及解决办法总结
  14. rviz显示矩形框BoundingBox
  15. 基于PHP+MySQL长途客用汽车票订票系统的设计与实现
  16. delphi 调用带有返回值的sql SERver 2008 存储过程
  17. 文件字符编码导致的VC多字符乱码解决
  18. 骑行天下之北京到天津(1)
  19. 新手解决Connections could not be acquired from the underlying database!Java代码上传到服务器连接不上数据库
  20. 2023 年腾讯云服务器租用价格表出炉(CPU、内存、带宽、系统盘)

热门文章

  1. boost::math::geometric相关用法的测试程序
  2. Boost::context模块callcc的斐波那契测试程序
  3. boost::callable_traits的has_varargs的测试程序
  4. VTK:可视化之AnnotatedCubeActor
  5. VTK:Picking之HighlightSelection
  6. OpenCV图像修补
  7. OpenCV HDF和建立群组Group
  8. Qt Creator造型Modeling
  9. OpenGL starfield星空的实例
  10. QT的QAction类的使用