[LeetCode&Python] Problem 1: Two Sum
Problem 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].
Approach 1: Brute Force
At the beginning, I want to use the Brute Froce method to use this problem. Then I write this code:
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n=len(nums) for j in range(n):
for i in range(j+1,n):
if nums[j]+nums[i]==target:
return j,i
However, this method wastes too much time.
Solution:
A better method to solve this problem is to use a dictionary to store all the pairs' indices.
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n=len(nums) d={} for x in range(n):
a = target-nums[x]
if nums[x] in d:
return d[nums[x]],x
else:
d[a]=x
[LeetCode&Python] Problem 1: Two Sum的更多相关文章
- [LeetCode&Python] Problem 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 ...
 - [LeetCode&Python] Problem 653. Two Sum IV - Input is a BST
		
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
 - [LeetCode&Python] Problem 371. Sum of Two Integers
		
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
 - [LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists
		
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...
 - [LeetCode&Python] Problem 404. Sum of Left Leaves
		
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
 - [LeetCode&Python] Problem 682. Baseball Game
		
You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...
 - [LeetCode&Python] Problem 883. Projection Area of 3D Shapes
		
On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each ...
 - [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline
		
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
 - 【leetcode❤python】 303. Range Sum Query - Immutable
		
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
 
随机推荐
- [LeetCode] 20. Valid Parentheses ☆
			
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
 - 谈一谈JUnit神奇的报错 java.lang.Exception:No tests found matching
			
最近在学习Spring+SpringMVC+MyBatis,一个人的挖掘过程确实有点艰难,尤其是有一些神奇的报错让你会很蛋疼.特别是接触一些框架还是最新版本的时候,会因为版本问题出现很多错误,欢迎大家 ...
 - startActivityForResult的用法,以及intent传递图片
			
开启startActivityForResult. Intent intent = new Intent(); intent.setClass(MainActivity.this, MipcaActi ...
 - Redis的JAVA连接
			
ShardedJedis用法 package com.zhi.demo; import java.util.Arrays; import java.util.List; import redis.cl ...
 - Astah Professional安装
			
asish安装 1● 文件下载 2● 安装图解 3● 破解 replace 4● 测试 success
 - asp.net mvc如何获取url的相关信息
			
1.获取完整url信息(协议名+域名+虚拟目录名+文件名+参数) string url = Request.Url.ToString(); 如: //1)获取完整url(协议名+域名+虚拟目录名+文件 ...
 - react router @4 和 vue路由 详解(全)
			
react router @4 和 vue路由 本文大纲: 1.vue路由基础和使用 2.react-router @4用法 3.什么是包容性路由?什么是排他性路由? 4.react路由有两个重要的属 ...
 - ES6中的Promise.resolve()的作用
			
var foo = { then: (resolve, reject) => resolve('foo') }; var resolved = Promise.resolve(foo); 相当于 ...
 - linux:centOs7换源阿里云
			
备份: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载: wget -O /etc/y ...
 - 向量空间模型(VSM)在文档相似度计算上的简单介绍
			
C#实现在: http://blog.csdn.net/Felomeng/archive/2009/03/25/4023990.aspx 向量空间模型(VSM:Vector space model)是 ...