Leetcode 1. Two Sum (Python)
refer to https://blog.csdn.net/linfeng886/article/details/79772348
Description
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
Python code
1. Brute Force
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
N = len(nums)
for i in range(N):
for j in range(i+1, N): # Traverse the next element in turn
if nums[j] == target - nums[i]:
return [i,j]
break
else:
continue
This methods have high computation complexity and memory storage.
2. one for loop
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
N = len(nums)
for i in range(N):
rest = target - nums[i]
if rest in nums:
j = nums.index(rest)
return [i,j]
break
else:
continue
3. create a dict first
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
N = len(nums)
d = {}
for i in range(N):
rest = target - nums[i]
if nums[i] in d:
return d[nums[i]], i
else:
d[rest] = i
Leetcode 1. Two Sum (Python)的更多相关文章
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- [LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [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 ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- 51nod1238 最小公倍数之和 V3(莫比乌斯反演)
题意 题目链接 Sol 不想打公式了,最后就是求一个 \(\sum_{i=1}^n ig(\frac{N}{i})\) \(g(i) = \sum_{i=1}^n \phi(i) i^2\) 拉个\( ...
- C#基础(203)实例方法和重载方法总结,构造方法与实例方法总结,this关键字
c#方法的重载:分为实例方法重载和静态方法重载俩种 1.实例方法重载的调用特点 首先写三个Add方法和三个Sub方法 public int Add(int a,int b) { return a + ...
- js判断当前内容是否为空
function isValue(o) { return (this.isObject(o) || this.isString(o) || this.isNumber(o) || this.isBoo ...
- div中img依据不同分辨率居中显示,超出部分隐藏
在做banner居中时 碰到的问题,知道可以用背景图实现居中显示,但是内心是想深究下的,故找到几种办法收集一下,后面两种真的是奇技淫巧 来着下面两处 https://www.zhihu.com/que ...
- Linux 安装 Mysql 5.7.23
切换目录 cd /usr 创建目录 mkdir mysql cd mysql 下载 Mysql Yum wget http://repo.mysql.com/mysql57-community-rel ...
- Android 线程交互
在Android开发过程中,耗时操作是不允许写在主线程(UI线程)中的,以免由于等待时间过长而发生ANR.所以耗时操作需要创建子线程来完成,然而往往这些操作都需要与主线程进行通讯交互(例如更新主线程的 ...
- 几个时间:UTC、GMT、本地时间、Unix时间戳
UTC(Coordinated Universal Time)时间:协调世界时,即世界标准时间 GMT(Greenwich Mean Time):格林威治/格林尼治时间 GMT=UTC,均使用秒数来计 ...
- 没有服务商如何购买ERP的序列号?
一.试用期(未过期) 站点版购买: 门店版购买: 二.试用期(使用时间<=15天) 三.试用期(已过期) 登录时会弹出以下弹窗 剩下的购买步骤与未过期时购买步骤一致 四.续费 剩下步骤与未过期时 ...
- PHP断言(ASSERT)的用法
简述 编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.程序员断言在程序中的某个特定点该的表达式值为真.如果该表达式为假,就中断操作. 可以 ...
- 剑指Offer 答题截图