LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs)
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
说明:
- 返回的下标值(index1 和 index2)不是从零开始的。
- 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
示例:
输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
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.
解题思路:
双指针例题的加强版:一个指针从左向右移动,另一个指针从右向左,左指针与右指针之和如果小于 target ,则左指针右移一位,如果大于target ,右指针左移一位,直到双指针之和等于target。
代码(java):
class Solution { public int[] twoSum(int[] numbers, int target) { int[] res = new int[2]; int i = 0, j = numbers.length - 1,temp;//i为左指针,j为右指针 while (i < j) { temp=numbers[i] + numbers[j];//先记录两数之和 if (temp == target) {//等于目标数则记录其索引 res[0] = i + 1; res[1] = j + 1; return res; } else if (temp < target) {//小于目标数,左指针右移一位 i++; } else {//大于目标数,右指针左移一位 j--; } } return null; } }
代码(python3):
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: i=0 j=len(numbers)-1 while i<j: if numbers[i]+numbers[j]==target: return i+1,j+1 elif numbers[i]+numbers[j]>target: j-=1 else:i+=1
总结:
这道题本身很简单,几个小细节:
-
if (temp == target)
先判断与目标数是否相同 可减少运行时间(因为Leetcode是拿很多不同数据来运行,而不是一条超长数据。仔细想想这里的区别) -
temp=numbers[i] + numbers[j]
先把两数之和记录下来,像py3里那种判断两次(==、>)每次都计算一次两数和,会消耗更多时间,这在判断条件增多时会很明显。
扩展:
后面找到py3
一种很有意思的解法,就是效率不高,扩展一下思路即可:
class Solution(object): def twoSum(self, numbers, target): s = {} r = [] for i in range(len(numbers)): if numbers[i] in s.keys():#判断该数在s键值对的键中是否存在。因为键值对的键记录的是差值 r.append(s[numbers[i]]+1) r.append(i+1) return r s[target-numbers[i]] = i#目标数与每一个数差值记录为s键值对的键,其索引记录为值 return None
利用py3字典特性解题,很有意思。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
突破Java面试(23-8) - Redis哨兵主备切换的数据丢失问题
Github 1 数据丢失的两个场景 主备切换的过程,可能会导致数据丢失 1.1 异步复制 由于 master => slave的复制是异步的,所以可能有部分数据还没复制到slave,master就宕机,于是这些数据就丢失了 1.2 脑裂导致 脑裂,也就是说,某个master所在节点突然脱离正常的网络,无法和其他slave机器连接,但实际上master还运行着 此时哨兵可能就会认为master宕机了,然后开启选举,将其他slave切换成了master 这个时候,集群里就会有两个master,也就是所谓的脑裂 此时虽然某个slave被切换成了master,但是可能client还没来得及切换到新的master,还继续写向旧master的数据可能也丢失了 因此旧master再次恢复时,会被作为一个slave挂到新的master上去,自己的数据会被清空
- 下一篇
阿里云人脸识别公测使用说明
概述 之前阿里云人脸识别只提供人脸检测,人脸属性及人脸对比三个API接口,关于这方面的介绍及使用细节,可以参考阿里云人脸识别使用流程简介,之前使用的服务地址为:dtplus-cn-shanghai.data.aliyuncs.com。目前新版本加入了1:N人脸查找的功能,新版本还处于公测阶段,服务地址:face.cn-shanghai.aliyuncs.com。下面主要介绍如何使用新版本的地址调用之前的三个API的功能。 使用流程 1、服务开通及1:N人脸识别使用阿里云人脸识别 1:N 使用简明示例 2、接口调用Code示例 import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient;
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器