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. jmeter接口测试与接口测试工具

    接口测试与接口测试工具 1,什么是接口? 接口,Application Programming Interface(API) 通俗的讲 就是HTTP请求 2,什么是接口测试? 接口测试测试组件间接口的 ...

  2. 关闭本机的代理服务(Proxy)

    若您使用了代理服务(Proxy),可能会导致战网游戏发生网络连接.安装或更新方面的问题.请参考以下步骤来关闭您电脑的网络代理服务. Windows 按下 Windows 按鍵 + R . 在运行框中, ...

  3. Alisha’s Party

    Alisha’s Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. python之-框架

    MVC:Model-View-Controller,中文名“模型-视图-控制器” C——Python处理URL的函数:Controller,Controller负责业务逻辑,比如检查用户名是否存在,取 ...

  5. python发送消息到ipmsg

    from socket import * #利用socket模块生成套接字s = socket(AF_INET,SOCK_DGRAM) #定义一个元组,包含ip地址,和端口号,ip地址必须为字符串,端 ...

  6. ubuntu 配置pptp

    PPTP是点对点隧道协议,用于在公网上建立两个节点之间的专用用网络.普通的用户一般是通过拨号的方式,接入ISP提供的网络,由于国内的上网环境,是访问不了google的,所以必须首先要有一台可以上goo ...

  7. tools-eclipse-003-下载安装优化

    一.下载 1.1.jdk安装 下载:http://www.oracle.com/technetwork/java/javase/downloads/index.html 安装:不需要安装jre,默认会 ...

  8. 从零搭建一个Redis服务

    前言 自己在搭建redis服务的时候碰到一些问题,好多人只告诉你怎么成功搭建,但是并没有整理过程中遇到的问题,所有楼主就花了点时间来整理下. linux环境安装redis 安装中的碰到的问题和解决办法 ...

  9. 状压DP : [USACO06NOV]玉米田

    玉米田 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ ...

  10. java--反射原理及操作

    1.反射原理 反射具体操作 15.反射的原理(********理解********) * 应用在一些通用性比较高的代码 中 * 后面学到的框架,大多数都是使用反射来实现的 * 在框架开发中,都是基于配 ...