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. 在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true

    Boolean 表达式 一个值为 true 或者 false 的表达式.如果需要,非 Boolean 表达式也可以被转换为 Boolean 值,但是要遵循下列规则: 所有的对象都被当作 true. 当 ...

  2. java 类变量初始化顺序

    假定有一个类定义如下: package com.zhang; public final class Girl { // static代码块1 private static String sex = & ...

  3. 用highchaarts做股票分时图

    1.首先向社区致敬给予灵感参考: https://bbs.hcharts.cn/thread-1985-1-1.html(给予参考的的例子js配置代码未进行压缩,可以清楚看到配置信息)   2.公司是 ...

  4. CCF关于对NOIP2018复赛违规处罚的公告

    NOIP2018复赛于11月10-11日在全国31个赛区同时举行,现已结束.总体有序,但也有赛区出现违规现象.现将复赛中违规情况进行通报. 一.数据提交情况 CCF要求NOI各省组织单位在考试结束后在 ...

  5. [POJ3481]Double Queue

    Problem 0 结束操作 1 K P 将一个数K以优先级P加入 2 取出优先级最高的那个数 3 取出优先级最低的那个数 Solution Splay模板题 Notice 是输出数而不是输出优先级. ...

  6. jenkins部署java项目,脚本文件放在远程仓库中 和jar一起打包(六)

    jenkins部署java项目到远程linux上,脚本文件和项目一起上传到gogs上,直接执行gogs上的脚本文件来执行项目 (1)新建maven项目 pom.xml的配置 <project x ...

  7. Guns后台管理系统框架(毕业设计神器)

    Guns后台管理系统, 基于Spring Boot + Maven构建  + MyBatis + MySql数据库 导入Eclipse即可使用 十分钟即可搞定,做毕业设计的好帮手啊 最终效果图 登陆页 ...

  8. Java:将Excel数据导入到数据库(一眼就看会)

    所用Jar包 1. sqljdbc4.jar 连接数据库的Jar包(根据数据库的不同进行选择,我用的SqlServer2008) 2.Jxl.jar 访问Excel的Jar包 注意:支持以.xls结尾 ...

  9. easyui弹框后销毁当前tab弹框不显示的解决方式

    var id=$("#pageId").val(); var message = "{\"id\":" + id+ ",\&quo ...

  10. spring相关jar包的作用讲解(转)

    Spring各个jar包作用 原文链接:https://www.cnblogs.com/1995hxt/p/5255210.html    Spring AOP:Spring的面向切面编程,提供AOP ...