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 ...
随机推荐
- 生理周期POJ 1006
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138101 Accepted: 44225 Description 人生 ...
- mybatis 中 使用 allowMultiQueries=true
单条的数据进行修改或者插入的时候没问题,但是进行批量操作的时候就会出现错误,是因为没有开启支持批量操作的功能. mybatis支持批量操作 开启批量执行sql的开关,在拼装mysql链接的url时,为 ...
- Unity3D手机斗地主游戏开发实战(02)_叫地主功能实现
大体思路 前面我们实现了点击开始游戏按钮,系统依次给玩家发牌的逻辑和动画,并展示当前的手牌.这期我们继续实现接下来的功能--叫地主. 1.首先这两天,学习了DOTween,这是一个强大的Unity动画 ...
- web-worker 的使用
JavaScript采用的是单线程模式,它每次也只能执行一个事件,所以它在加载大量的事件的时候会比较慢. 而web-worker的作用就是给JavaScript提供一个多线程的模式. 注意的是 web ...
- 在插件中得到,调用 插件的id
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- DAY14(PYTHONS)生成器进阶
def average(): sum = 0 count = 0 avg = 0 while True: #循环 num = yield avg #遇到yield就停止,防止一开始除数为0导致崩溃 s ...
- android修改getprop读取到的ro.build.fingerprint属性
在build/tools/buildinfo.sh中定义ro.build.fingerprint=$BUILD_FINGERPRINT. 然后在build/core/Makefile中给BUILD_F ...
- macOS下MySQL 8.0 安装与配置教程
一.前言 1.本教程主要内容 适用Homebrew安装MySQL MySQL 8.0 基础适用于配置 MySQL shell管理常用语法示例(用户.权限等) MySQL字符编码配置 MySQL远程访问 ...
- CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6
CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6 解决办法: 1.进入/etc/sysconfig/network-script ...
- c/c++ 智能指针 weak_ptr 使用
智能指针 weak_ptr 使用 weak_ptr用途: 1,解决空悬指针问题 2,解决循环引用问题 weak_ptr特点:没有*操作和->操作 weak_ptr是不控制所指对象生存周期的智能指 ...