【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 ...
随机推荐
- mysql proxy 数据库读写分离字符集乱码
mysql proxy 数据库读写分离字符集乱码 解决办法 在对应配置后端数据库服务器的配置.cnf中加入如下代码 init-connect='SET NAME UTF8' skip-characte ...
- Python3调用C程序(超详解)
Python3调用C程序(超详解) Python为什么要调用C? 1.要提高代码的运算速度,C比Python快50倍以上 2.对于C语言里很多传统类库,不想用Python重写,想对从内存到文件接口这样 ...
- Learning Spark中文版--第四章--使用键值对(2)
Actions Available on Pair RDDs (键值对RDD可用的action) 和transformation(转换)一样,键值对RDD也可以使用基础RDD上的action(开工 ...
- 大数据学习day32-----spark12-----1. sparkstreaming(1.1简介,1.2 sparkstreaming入门程序(统计单词个数,updateStageByKey的用法,1.3 SparkStreaming整合Kafka,1.4 SparkStreaming获取KafkaRDD的偏移量,并将偏移量写入kafka中)
1. Spark Streaming 1.1 简介(来源:spark官网介绍) Spark Streaming是Spark Core API的扩展,其是支持可伸缩.高吞吐量.容错的实时数据流处理.Sp ...
- spring boot-jpa整合QueryDSL来简化复杂操作
spring boot-jpa整合QueryDSL来简化复杂操作 SpringDataJPA+QueryDSL玩转态动条件/投影查询
- HelloWorldModelMBean
package mbeanTest; import java.lang.reflect.Constructor; import javax.management.Descriptor; import ...
- java Map集合类
---恢复内容开始--- Map提供了一个更通用的元素存储方法,Map集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值. 了解Map接口和方法 ...
- HashMap 和 HashSet
对于HashSet而言,系统采用Hash算法决定集合元素的存储位置,这样可以保证快速存取集合元素: 对于HashMap,系统将value当成key的附属,系统根据Hash算法来决定key的存储位置,这 ...
- Echarts 实现tooltip自动显示自动播放
1.其实这个很容易实现,一个 dispatchAction 方法就解决问题:但是博主在未实现该功能时是花了大力气,各种百度,各种搜: 很难找到简单粗暴的例子,大多数随便回一句你的问题就没下文: 废话太 ...
- Mysql资料 存储索引