1 Two Sum

**Difficulty: Easy **

The Link:

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

Solutions

Solution A:

暴力解法 two-loop runtime:7496ms,

class Solution:
def twoSum(self,num,target):
"""
:type nums:List[int]
:type targrt: int
:type: Link[int]
"""
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] + nums[j] == target
return [i,j]
return []

Solution B:

By dict to improve the effiency of the programs.Runtime:34ms

data :       2     7   11   12   17
i : 0 1
target-num: 7 2
look_up: {} {2:0}
result: no {1,0}
class Solution:
def twoSum(self,num,target):
"""
:type nums:List[int]
:type targrt: int
:type: Link[int]
"""
lookup = {}
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
return []

leetcode 001的更多相关文章

  1. [leetCode][001] Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  2. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...

  3. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  4. Leetcode 001. 两数之和(扩展)

    1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...

  5. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. two Sum ---- LeetCode 001

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. leetcode 001 Two Sun

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

随机推荐

  1. Apache HttpClient之fluent API的使用

    该方法为Apache HttpClient 4.5以上的版本支持,在官网有明确的说明. 对比以前的方式,其优点是代码更简洁,同时为线程安全的.仅举一个最简单的post栗子 JAR包信息: <de ...

  2. BZOJ 3306: 树 LCT + set 维护子树信息

    可以作为 LCT 维护子树信息的模板,写的还是比较优美的. 本地可过,bzoj 时限太紧,一直 TLE #include<bits/stdc++.h> #define setIO(s) f ...

  3. GIS矢量大数据采集

    1.使用什么工具采集 2.在哪个网站采集 3.采集哪一种数据 >>地理大数据公众号 >>大数据公众号 >>智能数据湖公众号 点.线.面.体 可视化 >> ...

  4. How to: Create a Windows Communication Foundation Client

    How to: Create a Windows Communication Foundation Client To create a Windows Communication Foundatio ...

  5. 【Visual Studio】 使用EF、 Linq2Sql快速创建数据交互层(一)

    项目伊始,创建数据库交互层代码是底层框架的首要任务.常用的做法包括手动编码.Hibernate或者动软之类的代码生成器,而多数人忽略了.Net环境下VS提供的两套非常好用的数据层工具. EF和Linq ...

  6. java sftp判断目录是否存在

    java sftp判断目录是否存在 public boolean isExistDir(String path,ChannelSftp sftp){ boolean isExist=false; tr ...

  7. org.dom4j 解析XML

    org.dom4j 解析xml java 代码 1 import java.io.File; import java.io.FileOutputStream; import java.io.FileW ...

  8. 【MM系列】SAP KP26 报工出错

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP KP26 报工出错   前言 ...

  9. 单例模式(Singleton Patten)

    顾名思义,单例模式就是只有一个实例,不管怎样,使用了单例模式的类在系统中只有一个对象被访问到.Java中单例模式定义:“一个类有且仅有一个实例,并且这个类会自行实例化,实例化时候的对象可以提供给整个系 ...

  10. 20190820 On Java8 第十章 接口

    第十章 接口 接口和抽象类提供了一种将接口与实现分离的更加结构化的方法. 抽象类和方法 包含抽象方法的类叫做抽象类.如果一个类包含一个或多个抽象方法,那么类本身也必须限定为抽象的,否则,编译器会报错. ...