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 ...
随机推荐
- 2018-08-06 在Office的VBA代码里中文命名
在Excel处理数据时, 顺便试了一下VBA代码编辑器里输入中文, 结果显示为乱码. 查了一下发现VBA本身支持Unicode, 但需要设置系统配置使编辑器能够正常显示, 即设置简体中文为Curren ...
- 如何获取view的大小
很多初学者都会犯一个错误 ,就是在onCreate或者onStart里面去获取view的大小,然而这样获取到的宽高通常都是0,为什么呢?因为view的测量过程和activity的生命周期不是同步的,因 ...
- Implemented the “Importance Sampling of Reflections from Hair Fibers”
Just the indirect specular pass by importance sampling. With all layers. Manually traced by 3D Ham ...
- 浅谈servlet与jsp的关系
servlet是用java语言编写的,是一个java类.主要功能是用来接受.处理客户端的请求,并把处理结果返回到客户端显示.Jsp是servlet发展后期的产物.在没有jsp之前,servlet利用输 ...
- TURN Server Windows 安装程序
有了OfficeSIP TURN Server 安装包,记录一下. http://www.onlinedown.net/soft/94746.htm 开源代码(C#)和应用地址:https://sou ...
- coTurn 运行在Windows平台的方法及服务与客户端运行交互流程和原理
coTurn是一个开源的STUN和TURN及ICE服务项目,只是不支持Windows.为了在window平台上使用coTurn源码,需要在windows平台下安装Cygwin环境,并编译coTurn源 ...
- SQL Server 2012还原一直卡在ASYNC_IO_COMPLETION浅析
在SQL Server 2012(11.0.7001.0)下面在还原一个数据库(备份文件40多G大小,实际数据库大小300G),在还原过程中,出现一直等待ASYNC_IO_COMPLETION,如下测 ...
- The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction.
同事反馈一个系统在运行一个存储过程时遇到了下面错误: Msg 1206, Level 18, State 169, Procedure xxxxxx, Line 118The Microsoft Di ...
- iis 限制动态IP地址访问次数
An IP Address Blocking HttpModule for ASP.NET in 9 minutes namespace YourModuleNameHere 10 { 11 publ ...
- 利用dockerfile制作基于centos7的lnmp镜像(亲测,详细版)
首先呢,这篇文章,也是小弟参考了许多文章,自己整理出来的,有很多不足之处还有待加强,期待各位评论. > LNMP 是代表 Linux 系统下的 Nginx.Mariadb.PHP 相结合而构建成 ...