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. Minimum supported Gradle version is 4.1. Current version is 2.14.1

    Error:Minimum supported Gradle version is 4.1. Current version is 2.14.1. If using the gradle wrappe ...

  2. python 关闭垃圾回收

    import gc gc.disable() http://blog.csdn.net/aixiaohei/article/details/6446869

  3. Win10系列:JavaScript动画4

    上面介绍的动画效果是通过Windows动画库创建的,这里的旋转动画是通过设置页面元素的style对象的相关属性来创建,此动画的效果是将界面元素沿着指定的方向进行旋转.下面介绍style对象的几个常用属 ...

  4. Java反射《四》获取方法

    package com.study.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.refl ...

  5. day 09 初识函数

    今日主要学习了 一. 什么是函数二. 函数定义, 函数名, 函数体以及函数的调?三. 函数的返回值四. 函数的参数 一, 什么是函数               如果找不到合适的函数名称 ,用 fu ...

  6. es6-priomise

    Promise是异步编程的一种解决方案,它有三种状态,分别是 pending-进行中 resolved-已完成 rejected-已失败 状态一旦改变,就无法再次改变状态,这也是它名字promise- ...

  7. 深入理解java虚拟机---java虚拟机内存管理(七)

    本地方法栈.java堆.方法区 本地方法栈在HotSpot版本内与java虚拟机栈是合二为一的.不单独区分本地方法栈.但是java虚拟机中是有这样一块区域的. 作用: 1.本地方法栈为虚拟机栈执行ja ...

  8. docker 安全性问题

    最近项目组成员要在k8s中引入类似于docker --privileged 的功能.显示通过api查询在container和pod层面做了securityContext的设置. 但是没有起到效果.于是 ...

  9. Oracle备份

    今天被吊,特来学习备份. https://blog.csdn.net/zhaiqi618/article/details/5616215 https://www.cnblogs.com/yingpp/ ...

  10. 2019-03-08 RF 调试Zibee 数据