【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/#/description
题目描述
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.
题目大意
在已经排好序的数组中,找到两个数字的和等于target,返回两个数字的基于1开始的索引。
解题方法
Java解法
已经排序好了的数组,找出目标和。可以挨个试,时间复杂度是O(n^2),复杂度太高。可以用双指针,同时向中间靠拢,肯定能达到目标的和。
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int index[] = new int[2];
int left = 0;
int right = numbers.length - 1;
while(left < right){
long temp = numbers[left] + numbers[right];
if(temp == target){
index[0] = left + 1;
index[1] = right + 1;
break;
}else if(temp < target){
left++;
}else{
right--;
}
}
return index;
}
}
Python解法
二刷,python解法。
class Solution:
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
N = len(numbers)
left, right = 0, N - 1
while left < right:
cursum = numbers[left] + numbers[right]
if cursum == target:
return [left + 1, right + 1]
elif cursum < target:
left += 1
else:
right -= 1
return [0, 0]
日期
2017 年 4 月 18 日
2018 年 11 月 14 日 —— 又到周五了
【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)的更多相关文章
- 29. leetcode 167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告
1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...
- 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 that the ...
- (双指针 二分) 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 that the ...
- LeetCode 167 Two Sum II - Input array is sorted
Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...
- ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Java [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 th ...
- 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 that the ...
随机推荐
- eclipse 配置黑色主题(转载)
转载:http://www.cnblogs.com/csulennon/p/4231405.html 虽然以前也使用eclipse的黑色主题,但是配置起来稍微麻烦一点. 这里先声明,下面的方式适合最新 ...
- CentOS6源码安装zabbix服务器
1.下载安装包并解压 2.预环境搭建 3.创建zabbix用户,编译安装zabbix 4.配置mysql 5.配置zabbix-server 6.配置apache和php 7.添加开机自启动 1 yu ...
- mysql 中@ 和 @@的区别
@x 是 用户自定义的变量 (User variables are written as @var_name)@@x 是 global或session变量 (@@global @@session )@ ...
- RocketMQ这样做,压测后性能提高30%
从官方这边获悉,RocketMQ在4.9.1版本中对消息发送进行了大量的优化,性能提升十分显著,接下来请跟着我一起来欣赏大神们的杰作. 根据RocketMQ4.9.1的更新日志,我们从中提取到关于消息 ...
- FTP 文件传输服务
昨晚心血来潮,尝试用python写了一个ftp文件传输服务,可以接收指令,从远程ftp服务器同步指定目录数据,最后没用上,开源出来. https://github.com/jadepeng/ftp_t ...
- Maven 目录结构[转载]
转载至:http://www.cnblogs.com/haippy/archive/2012/07/05/2577233.html Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目, ...
- 集合类——Map集合、Properties属性文件操作
1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...
- 【编程思想】【设计模式】【行为模式Behavioral】备忘录模式Memento
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/memento.py #!/usr/bin/env pyt ...
- 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...
- 【Linux】【Services】任务计划、周期性任务执行
Linux任务计划.周期性任务执行 未来的某时间点执行一次某任务:at, batch 周期性运行某任务:crontab 执行结果:会通过邮件发送给用户 ...