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 ...
随机推荐
- cell内存优化
UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewC ...
- PL/SQL基础知识
Oracle之PL/SQL学习笔记 自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了 ...
- Validation(2)
站在巨人的肩膀上 spring注解式参数校验 2016年06月15日 15:42:47 God_Ming 阅读数:57021 标签: springhibernatevalidator 更多 个人分类: ...
- jmeter常用的beanshell脚本
时间戳下载文件保存响应内容断言连接数据库解析jsonlist递归创建多级目录 常用内置变量调用cmd文件GUI小命令 时间戳import java.text.SimpleDateFormat;impo ...
- Ubuntu使用实录
在实验室的电脑上重新配置了Linux开发环境,使用的是Ubuntu 14.04.5 LTS. 在开发中遇到的问题甚多,一一记录如下: 1.切换为root身份 先给root用户设定密码,然后进行切换 s ...
- Hive_Hive的管理_web界面方式
端口:9999启动方式: hive --service hwi &通过浏览器访问:http://<IP地址>:9999/hwi/ 执行启动命令后,报错,找不到hive-hwi-*. ...
- vue初级学习--使用 vue-resource 请求数据
一.导语 我发现好像我最近几次写文,都是在7号,很恰巧啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- rsync服务的安装与配置
rsync 服务的安装配置与客户端的同步操作 1. 使用xinetd服务运行rsync服务: 服务器端: 1.关闭selinux,设置iptables开放xinetd的873端口 2. yum - ...
- 转 shell模拟数据库的读写
0.create table create table myTestTable as select rownum as id, to_char(sysdate + rown ...
- php 分析2
a:link,a:visited,a:hover,a:active 1:解释 link:连接平常的状态 visited:连接被访问过之后 hover:鼠标放到连接上的时候 active:连接被按下 ...