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.
 class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
my_dict = {}
for i, num in enumerate(numbers):
remain = target - num
if remain in my_dict:
index = my_dict[remain]
return [index, i + 1]
my_dict[num] = i + 1
return [-1, -1]

[LC] 167. Two Sum II - Input array is sorted的更多相关文章

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

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

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

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

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

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

随机推荐

  1. Spring Cloud Alibaba 教程 | 前世今生

    Spring Cloud Alibaba是什么 先来看一下官方是怎么定义Spring Cloud Alibaba的: Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此 ...

  2. ABP JTable如何显示序列号

    显示序列号 recordsLoaded: function (event, data) { var SrNo = 0; if (data.records) { $.each(data.records, ...

  3. 5)添加一个tab

    1)还是按照(4)的代码:2) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~ ...

  4. Mybatis之二级缓存(八)

    1. 介绍 Mybatis缓存分为一级缓存和二级缓存,在本节中我们介绍下二级缓存的使用及其特性 MyBatis的一级缓存是在一个Session域内有效的,当Session关闭后,缓存内容也随之销毁.但 ...

  5. KAFKA伪集群单机安装

    下载 kafka_2.11-2.0.1.tgz 文档kafka_2.11-2.0.1-site-docs.tgz cd /uae/local tar -zxvf kafka_2.11-2.0.1.tg ...

  6. XCOM串口助手打印不出数据

    本次实验是在基于原子的战舰开发板上的做定时器捕获实验,程序源码下载到板子上运行正常.指示灯正常显示,打开XCOM识别不来串口,原因:硬件上没有插USB转串口线: 连接上USB转串口线,软件上以显示CH ...

  7. 苹果智能AR挡风玻璃靠谱吗?

    在过去十年,外界给苹果的形象一直是"伟大的硬件公司",他们的产品在外观方面往往比内涵更加引人注目,兼具娱乐性和艺术性, iPhone/iPad/iPod莫不如此,所以,当坊间传闻苹 ...

  8. Java中包的基本管理与编译

    在写程序的过程中,总会出现代码编译过关,但是项目偏偏报错的情况,遇到几种情况,都在此一一记录,希望以后少走弯路. 1.添加jsp文件的时候,会报错 Multiple annotations found ...

  9. Linux用户权限常见命令

    01. 用户 和 权限 的基本概念 1.1 基本概念 用户 是 Linux 系统工作中重要的一环,用户管理包括 用户 与 组 管理 在 Linux 系统中,不论是由本机或是远程登录系统,每个系统都必须 ...

  10. 如何实现MVC ActionResult 返回类型为JavaScriptResult

    必需的js引用文件 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>@Scripts ...