LeetCode 167.Two Sum II 解题报告

题目描述

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.


示例

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2


限制条件

没有明确给出。


解题思路

题目说了数组是排序的,这样难度就降低了。由于数组是升序排列,所以两个数之和sumsum为目标值targettarget,则肯定是小的数在前,大的数在后,这样明显是双指针的问题。
建立一个指针指向数组第一个元素,建立另一个指针指向数组最后一个元素,为了方便,暂时称为左右指针,通过将它们的和与目标值比较,有三种情况:

  • sum>targetsum \gt target,说明右指针指向的元素过大,所以向左移动右指针。
  • sum<targetsum \lt target,说明左指针指向的元素过小,所以向右移动左指针。
  • sum=targetsum = target,找到了和为targettarget的两个数的索引,返回这两个索引。
    通过一个循环,重复上述的情况检查,循环结束的条件是左指针指向的位置不再小于右指针指向的位置。

代码

class Solution {
public:vector<int> twoSum(vector<int>& numbers, int target) {vector<int> indexes;int left = 0;int right = numbers.size() - 1;int sum = 0;while(left < right) {sum = numbers[left] + numbers[right];if (sum == target) {indexes.push_back(left + 1);indexes.push_back(right + 1);break;} else if (sum < target) {left++;} else {right--;}}return indexes;}
};

总结

双指针的问题还是比较容易处理的,关键是确定好指针更新的条件,以及结束移动指针的条件。
今天又遇到了一道双指针的题目,同样地当把双指针的题目都做完了会写个小小的总结,当做复习整理。继续不怀好意地盯着下一个坑,嘻嘻嘻~~

LeetCode 167.Two Sum II 解题报告相关推荐

  1. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

  2. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

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

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

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

  5. [leetcode] 213. House Robber II 解题报告

    题目链接:https://leetcode.com/problems/house-robber-ii/ Note: This is an extension of House Robber. Afte ...

  6. [Leetcode] 212. Word Search II 解题报告

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  7. [LeetCode]844. Backspace String Compare 解题报告(C++)

    [LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...

  8. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  9. [LeetCode]113.Path Sum II

    [题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...

最新文章

  1. MERGE INTO 解决大数据量 10w 更新缓慢的问题
  2. 有赞多级缓存解决方案怎么做的,你知道吗?
  3. SAP中手工配置Delivery打印message
  4. C++static关键字
  5. 从电子工程师到研发经理到老板的多面人生
  6. python __call__一般用在哪些地方_Python __call__内置函数的作用和用法
  7. python--装饰器、生成器、迭代器、元类
  8. opencv python教程简书_OpenCV-Python系列二:常用的图像属性
  9. Nginx + IIS实现负载均衡 Session多站点共享
  10. DiscuzNT 1.0正式版推出了
  11. 学python要有多少英语词汇量测试_非常适合新手的一个Python爬虫项目: 打造一个英文词汇量测试脚本!...
  12. 蚂蚁员工持股平台管理权变更 马云持股降至34%
  13. Unity开发《一起来捉妖》教程 | 3.随机妖怪位置及旋转提示
  14. python学习课后练习题_python初步学习-练习题
  15. 西瓜书读书笔记5-决策树的分裂原则
  16. C语言常见问题(4):Collapsible if statements should be merged
  17. 认真学习设计模式之适配器模式(Adapter Pattern)/包装器模式
  18. 离散数学总复习精华版(最全 最简单易懂)已完结
  19. api.php(260x260),Restful Api文档 -V3-教程
  20. 梅科尔工作室苏慎臻,Django使用ORM增删改

热门文章

  1. java中感叹号啥意思_感叹号暗示什么意思
  2. 火遍全球家喻户晓的小游戏——开心消消乐
  3. c语言字符串dna,DNA (C语言代码)
  4. 【概念】等位基因相关概念辨析
  5. TLS1之__thread
  6. PMO如何有效搭建项目管理体系︱伯俊软件PMO经理李双燕
  7. 上兵伐谋,其次伐交,其次伐兵,其下攻城
  8. echart,highcharts,chart.js等chart属性记录
  9. 什么是高性能计算,涉及哪些技术和知识呢?
  10. 英语作文计算机国际会议开幕词,英语学术会议开幕词