【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 ...
随机推荐
- day33 前端之css
day33 前端之css css简介 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. # 语法结构 选择器 { 属性名1,属性值 属性名2,属性值 } # ...
- 连接查询条件在on后面和条件在where后面
emp表结构如下: dept表结构如下: 内连接 条件语句放在on 后面和 where 结果对于inner join结果是一样的 但对于left join 结果会产生不一样 这种现象也比较好理解,如果 ...
- 转 proguard 混淆工具的用法 (适用于初学者参考)
转自:https://www.cnblogs.com/lmq3321/p/10320671.html 一. ProGuard简介 附:proGuard官网 因为Java代码是非常容易反编码的,况且An ...
- spring生成EntityManagerFactory的三种方式
spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...
- VFL
VFL 1. 概念 VFL全称是Visual Format Language,翻译过来是"可视化格式语言" VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 2. ...
- shell条件测试语句实例-测试apache是否开启
终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...
- canal安装与使用
安装 alpha的版本不是稳定的版本 wget https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.deploye ...
- JSP 文字乱码、${}引用无效
问题: 代码:<form action="/test/requestPost.do" method="post"> <input type=& ...
- Python模块和函数
目录 一.基础 二.特殊函数 一.基础 #导入模块 import xxx #调用 xxx.dd() from xxx import xx as dd #导入某个函数,as给函数加别名,调用xx() ...
- 测试工具_http_load
目录 一.简介 二.例子 三.参数 一.简介 http_load以并行复用的方式运行,用以测试Web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,其可以以一个单一的进程运行,这样就不会把客户机 ...