167. Two Sum II - Input array is sorted@python
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.
原题地址: Two Sum II - Input array is sorted
难度: Easy
思路1:
采用缓存的方式,用字典记录每一个值的索引
代码:
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
for idx, num in enumerate(numbers):
if target - num in d:
idx1 = d[target-num]
return [idx1+1, idx+1]
else:
d[num] = idx
时间复杂度: O(n)
空间复杂度: O(n)
思路2:
因为数组已经排序了,可以分别在数组首尾放置一指针,逐渐向中间夹逼
代码:
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
i, j = 0, len(numbers)-1
while i < j:
val = numbers[i] + numbers[j]
if val == target:
return [i+1, j+1]
elif val > target:
j -= 1
else:
i += 1
时间复杂度: O(n)
空间复杂度: O(1)
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 ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- [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 ...
- 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 ...
- (双指针 二分) 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 ...
- 167. Two Sum II - Input array is sorted (Array)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode&Python] Problem 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 派生表(Derived Table) Merge Optimization
本文将通过演示告诉你:MySQL中派生表(Derived Table)是什么?以及MySQL对它的优化. Background 有如下一张表: mysql> desc city; +------ ...
- 笔记-JavaWeb学习之旅7
JavaScript基础 概念:一门客户端脚本语言,运行在客户端浏览器中,每一个浏览器都有JavaScript的解析引擎,是一个脚本语言,不需要编译,直接就可以被浏览器解析执行. JavaScript ...
- MarkdownPad - win10环境下无法渲染HTML问题
问题 在win10平台安装了MarkdownPad 2之后,发现在渲染md文件时报错,在预览页面无法正常渲染HTML: 安装报错提示前往官网,可以看到如下的解决方法: LivePreview is n ...
- [题解]区间dp_luogu_P3147 262144
小数据版本P3146,可以区间dp, 性质:对于一个区间如果能合并成一个数,那么这个数是确定的 理解:把每个数看做 2^x 的形式,那么如果合并:2^x + 2^x =2^(x+1) 所以 f [ i ...
- HDU-1556:Color the ball(前缀和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- python之序列化json模块与pickle模块(待补充)
一.json是所有语言都通用的一种序列化格式 只支持 : 列表,字典字符串,数字,且字典的key必须是字符串 ''' 1. dumps , loads 在内存中做数据转换: dumps : 数据类型 ...
- 执行脚本 提示 command not found
问题现象: 初学shell,写了个脚本, 1.从windows 写好 脚本,然后部署到 linux 上. 2.chmod +x之后执行提示command not found,系统环境redhat9,用 ...
- D. Anton and Chess 模拟题 + 读题
http://codeforces.com/contest/734/problem/D 一开始的时候看不懂题目,以为象是中国象棋那样走,然后看不懂样例. 原来是走对角线的,长知识了. 所以我们就知道, ...
- dos命令安装windows服务
以下两种方法都是通过dos命令创建windows服务 1.创建服务 sc create UploadRealVolumeService start= auto binpath= C:\Users\Ad ...
- CF1079D Barcelonian Distance
思路: 模拟. 实现: #include <bits/stdc++.h> using namespace std; ; double dis(double x1, double y1, d ...