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:

  1. Input: numbers = [2,7,11,15], target = 9
  2. Output: [1,2]
  3. Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

原题地址: Two Sum II - Input array is sorted

难度: Easy

思路1:

采用缓存的方式,用字典记录每一个值的索引

代码:

  1. class Solution(object):
  2. def twoSum(self, numbers, target):
  3. """
  4. :type numbers: List[int]
  5. :type target: int
  6. :rtype: List[int]
  7. """
  8. d = {}
  9. for idx, num in enumerate(numbers):
  10. if target - num in d:
  11. idx1 = d[target-num]
  12. return [idx1+1, idx+1]
  13. else:
  14. d[num] = idx

时间复杂度: O(n)

空间复杂度: O(n)

思路2:

因为数组已经排序了,可以分别在数组首尾放置一指针,逐渐向中间夹逼

代码:

  1. class Solution(object):
  2. def twoSum(self, numbers, target):
  3. """
  4. :type numbers: List[int]
  5. :type target: int
  6. :rtype: List[int]
  7. """
  8. i, j = 0, len(numbers)-1
  9. while i < j:
  10. val = numbers[i] + numbers[j]
  11. if val == target:
  12. return [i+1, j+1]
  13. elif val > target:
  14. j -= 1
  15. else:
  16. i += 1

时间复杂度: O(n)

空间复杂度: O(1)

167. Two Sum II - Input array is sorted@python的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. (双指针 二分) 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. cell内存优化

    UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewC ...

  2. PL/SQL基础知识

    Oracle之PL/SQL学习笔记 自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了 ...

  3. Validation(2)

    站在巨人的肩膀上 spring注解式参数校验 2016年06月15日 15:42:47 God_Ming 阅读数:57021 标签: springhibernatevalidator 更多 个人分类: ...

  4. jmeter常用的beanshell脚本

    时间戳下载文件保存响应内容断言连接数据库解析jsonlist递归创建多级目录 常用内置变量调用cmd文件GUI小命令 时间戳import java.text.SimpleDateFormat;impo ...

  5. Ubuntu使用实录

    在实验室的电脑上重新配置了Linux开发环境,使用的是Ubuntu 14.04.5 LTS. 在开发中遇到的问题甚多,一一记录如下: 1.切换为root身份 先给root用户设定密码,然后进行切换 s ...

  6. Hive_Hive的管理_web界面方式

    端口:9999启动方式: hive --service hwi &通过浏览器访问:http://<IP地址>:9999/hwi/ 执行启动命令后,报错,找不到hive-hwi-*. ...

  7. vue初级学习--使用 vue-resource 请求数据

    一.导语 我发现好像我最近几次写文,都是在7号,很恰巧啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  8. rsync服务的安装与配置

    rsync 服务的安装配置与客户端的同步操作   1. 使用xinetd服务运行rsync服务: 服务器端: 1.关闭selinux,设置iptables开放xinetd的873端口 2. yum - ...

  9. 转 shell模拟数据库的读写

    0.create table create table myTestTable as select rownum as id,               to_char(sysdate + rown ...

  10. php 分析2

    a:link,a:visited,a:hover,a:active   1:解释 link:连接平常的状态 visited:连接被访问过之后 hover:鼠标放到连接上的时候 active:连接被按下 ...