[LC] 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 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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
随机推荐
- JAVA 算法练习(二)
和上次一样,虽说用 java 语言,但有 c 的基础一样可以看懂哦. 机器人走方格问题Ⅰ 题目概述 有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角.请设计一个算法,计 ...
- awk 中 RS,ORS,FS,OFS 区别与联系
一,RS与ORS 1,RS是记录分隔符,默认的分隔符是\n,具体用法看下 [root@krlcgcms01 mytest]# cat test1 //测试文件 111 222 333 444 ...
- Python说文解字_杂谈06
1. 序列类型的分类: 容器类型:list.tuple,deque 扁平序列:str.bytes.bytearray.array.array 可变序列:list.dequte.bytearray.ar ...
- Thinkcmf截取内容长度
例1: {$vo.post_title|msubstr=0,10} 截取标题,msubstr=0,10,数字表示截取的字符串长度,显示省略号,但无论长度是否超过截取的长度都会出现省略号: 例2: {$ ...
- 复杂分布式架构下的计算治理之路:计算中间件 Linkis
前言 在当前的复杂分布式架构环境下,服务治理已经大行其道.但目光往下一层,从上层 APP.Service,到底层计算引擎这一层面,却还是各个引擎各自为政,Client-Server 模式紧耦合满天飞的 ...
- Facebook的Libra “区块链”到底是如何运作的?
本文深入研究了"关于Facebook Libra coin (以及更多)平台协议"的26页技术文档,并对其内容进行了分解说明.同时,我们对这53位作者表示衷心的钦佩! 以下为具体分 ...
- Mac中制作USB系统启动盘
.iso镜像文件转 .dmg文件 hdiutil convert -format UDRW -o linuxmint.dmg ~/Desktop/linuxmint-19-cinnamon-64bit ...
- Spring MVC中防止csrf攻击
Spring MVC中防止csrf攻击的拦截器示例 https://blog.csdn.net/qq_40754259/article/details/80510088 Spring MVC中的CSR ...
- SSM到Spring Boot-校园商铺平台:第01章 开发准备
第01章 开发准备 环境准备 创建一个Maven项目作为开始 添加一个 Server Runtime 添加maven的java编译插件 <build> <finalName>$ ...
- nouveau :failed to create kernel chanel,-22
一:錯誤描述:今天在重啓 Ubuntu 的過程中,出現下圖的 grub 選項,系統重啓/開機之後出現以下畫面,然後選擇 Ubuntu 之後黑屏,提示錯誤:nouveau :failed to crea ...