解题思路:双指针 l 和 r,分别指向头和尾,对于这个有序数组,如果number[l] + number[r] 比 target大,那么说明选择的r对应元素偏大,将指针 r 左移;如果小于target ,那么相应的将指针 l 右移,直到两者相等!

vector<int> twoSum(vector<int> &numbers, int target)
{vector<int> res;int len = numbers.size();if (len == 0)return res;int l = 0, r = len - 1;while (l < r){if (numbers[l] + numbers[r] == target){res.push_back(l + 1);res.push_back(r + 1);break;}if (numbers[l] + numbers[r] > target){r--;continue;}if (numbers[l] + numbers[r] < target){l++;continue;}}return res;
}

Leetcode每日一题:167.two-sum-ii-input-array-is-sorted(两数之和Ⅱ-输入有序数组)相关推荐

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

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

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

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

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

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

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

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

  5. 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 ...

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

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

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

  8. [LeetCode By Python]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 ...

  9. 167. Two Sum II - Input array is sorted

    问题描述 解决方案 class Solution { public:vector<int> twoSum(vector<int>& numbers, int targe ...

最新文章

  1. 20145129 课程总结
  2. java web项目目录报错_netdevgirl.通过maven创建javaweb项目
  3. phpmailer 发送邮件空隙太大_WordPress纯代码无插件开启SMTP邮件服务——墨涩网
  4. Go语言编程:Go语言实现快速排序算法
  5. 【codevs30521022】多米诺覆盖,二分图
  6. 《Python从小白到大牛》第5章 Python编码规范
  7. mysql8.0依赖_分享MySql8.0.19 安装采坑记录
  8. 记录数据库内一条记录的更新时间
  9. 江苏高考成绩什么时候可以查询2021,2021年江苏高考成绩什么时候公布出来,几月几号几点钟可以查询...
  10. HTML+JS调用摄像头拍照并上传图片
  11. 电音插件auto_自动电音基调查询软件助手完美支持32_64bit系统所有电音插件
  12. 【知识分享】汽车搭载的车载摄像头分类
  13. Rosalind第16题——ros_bio16_MPRT
  14. 如何在word里插入矢量图
  15. 震旦复印机扫描到服务器文件夹,办公室复印机怎么扫描文件(图示复印机扫描功能应用)...
  16. python音乐编程_可以编程写音乐的python库musicpy教程(第一期) musicpy的数据结构...
  17. 报错:Unhandled exception
  18. 单个正态总体均值的区间估计_总体均值的区间估计 (正态总体: σ2 已知实例).pdf...
  19. php经常致命错误怎么办,如何捕获PHP中的致命错误(Fatal Errors)
  20. 电脑版android版iphone版ipad版,[下载]微软发布iPad版Office iPhone版Android版彻底免费...

热门文章

  1. 转载来自朱小厮的博客的NIO相关基础篇
  2. 【bzoj3122】 Sdoi2013—随机数生成器
  3. [读书笔记]读《Effective Objective-C 2.0编写高质量iOS与OS X代码的52个有效方法》(一)...
  4. Jquery常用方法合集,超实用
  5. js 去除字符串左右两边的空格
  6. 基于NSString处理文件的高级类
  7. 在ORACLE產生001,002的流水號
  8. 使用Apache NFOP创建pdf
  9. JDBC性能优化方案
  10. Mongodb总结1-启动和Shell脚本