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)的更多相关文章

  1. [leetcode]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  3. [LeetCode] 1. Two Sum 两数和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  4. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

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

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

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

  8. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

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

随机推荐

  1. git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:

    方法1:如果你想保留刚才本地修改的代码,并把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来) git stash git pull origin master git sta ...

  2. GitHub for Windows离线安装包

    国内安装github客户端,真的很痛!! 偶然找到了离线安装包,感谢作者的资源分享!!! 地址:http://download.csdn.net/download/lyg468088/8723039? ...

  3. 洛谷P2000 拯救世界(生成函数)

    题面 题目链接 Sol 生成函数入门题 至多为\(k\)就是\(\frac{1-x^{k+1}}{1-x}\) \(k\)的倍数就是\(\frac{1}{1-x^k}\) 化简完了就只剩下一个\(\f ...

  4. 【效率工具】史上最好用的SSH一键登录脚本,超强更新!

    说明 虽然已经是凌晨,但丝毫不能掩盖我激动的心情,今天完成了对GotoSSH的一次大更新,新增了两个肥肠实用的功能,我只能说,是真的好用,话不多说,先来看效果图: 普通的一键登录: 一键登录跳板机,然 ...

  5. 基于OpenSSL的HTTPS通信C++实现

      HTTPS是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL.Nebula是一个为开发者提供一个快速开发 ...

  6. Python textwrap模块(文本包装和填充)

    textwrap提供函数wrap().fill().indent().dedent()和以及TextWrapper类. 通常包装或者填充一两个字符串使用wrap()和fill().其他情况使用Text ...

  7. Keras实现VGG16

    一.代码实现 # -*- coding: utf-8 -*- """ Created on Sat Feb 9 15:33:39 2019 @author: zhen & ...

  8. Snapshot origin volumes can be resized only while inactive: try lvchange -an

    事件现象:   最近同事在扩展VG时遇到了"Snapshot origin volumes can be resized only while inactive: try lvchange ...

  9. Oracle解锁scott用户

    解决: (1)conn sys/sys as sysdba;//以DBA的身份登录 (2)alter user scott account unlock;// 然后解锁 (3)conn scott/t ...

  10. hive Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

    Exception in thread "main" java.lang.RuntimeException: org.apache.hadoop.hive.ql.metadata. ...