题目地址:Two Sum II - Input array is sorted - LeetCode


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.

Note:

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

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

这道题目最容易想到的是穷举法,但肯定会超时,而且没有利用有序这个特性。
然后就可以想到穷举的时候进行二分查找,这样就利用了有序这个特性。

Python解法如下:

class Solution:def twoSum(self, numbers: list, target: int) -> list:result = [0, 0]length = len(numbers)flag = 0def search(begin, end, number):nonlocal numbers, result, flagif begin > end:returnmiddle = (begin+end)//2if numbers[middle] == number:flag = 1result[1] = middle+1returnelif numbers[middle] < number:search(middle+1, end, number)else:search(begin, middle-1, number)for i in range(0, length-1):search(i+1, length-1, target-numbers[i])if flag == 1:result[0] = i+1return result

然后把递归的二分查找改为迭代的二分查找:

class Solution:def twoSum(self, numbers: list, target: int) -> list:result = [0, 0]length = len(numbers)flag = 0def search(begin, end, number):nonlocal numbers, result, flagwhile begin <= end:middle = (begin+end)//2if numbers[middle] == number:flag = 1result[1] = middle+1returnelif numbers[middle] < number:begin = middle+1else:end = middle-1for i in range(0, length-1):search(i+1, length-1, target-numbers[i])if flag == 1:result[0] = i+1return result

可以通过所有样例了,但结果不让人满意,随后可以想到既然是有序的,可以从两边向中间逼近:

class Solution:def twoSum(self, numbers: list, target: int) -> list:result = [0, 0]length = len(numbers)begin = 0end = length-1while begin < end:temp = numbers[begin]+numbers[end]if temp == target:result[0] = begin+1result[1] = end+1return resultelif temp > target:end -= 1else:begin += 1

这样时间复杂度就变为了O(N)。

LeetCode 167. Two Sum II - Input array is sorted--Python解法相关推荐

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

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

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

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

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

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

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

  7. LeetCode之Two Sum II - Input array is sorted

    1.题目 Given an array of integers that is already sorted in ascending order, find two numbers such tha ...

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

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

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

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

最新文章

  1. java基础系列:集合总结(4)
  2. MyBatis开发Dao的方法
  3. 浮点数可以直接相加么?_鸭粪屎可以直接做有机肥么?
  4. 一次对路边饮用水RFID供应机的跑路玩法
  5. Mysql 分组后组内排序按字段取最大或最小的数据
  6. java学习二---对象和内存管理
  7. IIS和APACHE共用80端口的方法
  8. 腾讯云总裁邱跃鹏:新基建最大的改变是从硬件到软件的转变
  9. Android底部日期控件,Android开发中实现IOS风格底部选择器(支持时间 日期 自定义)...
  10. 每天备份数据库中的表
  11. 插排与线分离设计的想法
  12. matlab7.0 win10安装报错,win10系统安装Matlab7.0后出现Runtime Error警告窗口的技巧介绍...
  13. linux ttl信号处理,TTL和带缓冲的TTL信号(详细)
  14. AES16位密钥加密解密
  15. 部分电商平台为防止爬虫竟然这样做?
  16. arm-linux平台的锐捷认证工具Mentohust移植
  17. java入门软件安装教程,PDPS软件-安装入门教程 20200406
  18. 架构师之路:从Java码农到年薪八十万的架构师
  19. maven项目查看依赖树
  20. 原型链上的__proto__和protoptype

热门文章

  1. TF-IDF模型的概率解释
  2. 怎么导入mysql示例_MySQL命令行导出导入数据库实例详解
  3. 24 式加速你的 Python
  4. 西农韦革宏组揭示甘草根系微生物群落分布及其与根内次级代谢产物之间的联系...
  5. Cell:康奈尔大学郭春君组开发针对非模式肠道细菌的基因编辑工具
  6. 招聘 | ​浙江农林大学孙学鹏团队招聘事业编制科研人员
  7. NAR:psRobot-植物小RNA分析系统
  8. 扩增子文献笔记1白杨内生和根际微生物组在不同生态位存在特异的群落结构
  9. python使用matplotlib可视化线图(line plot)、将可视化图像的图例(legend)放置在图像外部、右侧区域
  10. R语言广义线性模型函数GLM、广义线性模型(Generalized linear models)、GLM函数的语法形式、glm模型常用函数、常用连接函数、逻辑回归、泊松回归、系数解读、过散度分析