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

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

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

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

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

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

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

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

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

  9. 【leetcode❤python】 303. Range Sum Query - Immutable

    #-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...

随机推荐

  1. CentOS查看安装包会释放哪些文件

    1.查看软件包全称(以mysql为例) rpm -qa | grep -i mysql 2.查看释放出的文件(以MySQL-server-5.5.55-1.el6.x86_64为例) rpm -ql ...

  2. 使用javassist进行动态编程

    今天在研究dubbo时,发现一个新的知识点,可以使用javassist包进行动态编程,hibernate也使用该包进行编程.晚上百度了很多资料,将它的特性以代码的形式展现出来. package com ...

  3. How to calculate bits per character of a string? (bpc) to read

      http://stackoverflow.com/questions/17797922/how-to-calculate-bits-per-character-of-a-string-bpc up ...

  4. CAD(布置厨洁具)(尺寸标注)5.12

    "TYTK"打开图库,找到平面厨具和洁具.双击选中的厨具,A可以不停旋转90度.给厨具选取正确的位置.画出灶台线,同理画出卫生间的家具.绘制出洗脸台的平台.浴缸的平台. 尺寸标注: ...

  5. react之传递数据的几种方式props传值、路由传值、状态提升、redux、context

    react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...

  6. mac mysql 操作

    参考 http://www.cnblogs.com/chenmo-xpw/p/6102933.html http://www.cnblogs.com/uoar/p/6492521.html 1.启动M ...

  7. JAVA的环境变量配置(方式二)

    1.想要成功配置Java的环境变量,那肯定就要安装JDK(JDK安装包在方式一中),才能开始配置的. 2.安装JDK 向导进行相关参数设置.如图: 3.正在安装程序的相关功能,如图: 4.选择安装的路 ...

  8. python全栈开发笔记---基本数据类型--字符串魔法

    字符串: def capitalize(self, *args, **kwargs) test = "aLxs" v = test.capitalize() #capitalize ...

  9. day039 数据库索引

    今日内容: 1.为什么要有索引 简而言之,索引出现的意义是为了更方便,更快速的查询数据. 什么是索引 索引在mysql中也叫''键''或'key'(primary key unique key,ind ...

  10. loadrunner json中文无法识别问题

    http://blog.sina.com.cn/s/blog_6ff7a3b50101awmy.html