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. python基本数据预处理语法函数(2)

    1.字符串格式化方法format的用法: < ^ > #分别为左对齐.居中.右对齐 '{:>18,.2f}'.format(70305084.0) #:冒号+空白填充+右对齐+固定宽 ...

  2. YottaChain主网全面上线预示商业应用的落地区块链云存储不一样的云

    Yottachain存储网12月24日平安夜全面启动,意味着全球首个可商用的区块链存储公链全面落地.对于数据存储来说,小到我们个人的照片文档,大到政企机构数据库资料,都是互联网生活中如影随形的标配. ...

  3. python学习笔记(十)常用模块

    import os print(os.getcwd())#取当前工作目录,绝对路径 print(os.chdir("../"))#更改当前目录,.代表当前目录,..代表上一级目录 ...

  4. 基于Nginx和uWSGI在Ubuntu上部署Django项目

    前言: 对于做Django web项目的童鞋,重要性不言而喻. 参考:https://www.cnblogs.com/alwaysInMe/p/9096565.html https://blog.cs ...

  5. Android9.0特性

    这篇文章,是Android官方文档的中文版本. 注意事项(AndroidP 特性): (1),android.os.Build.VERSION.RELEASE ,需要当做字符串类型处理. (2),依赖 ...

  6. unique && stl的全排列

    stl的全排列: 看代码. #include<iostream> #include<cstdio> #include<algorithm> #include< ...

  7. Gym - 101194H Great Cells

    Problem H. Great Cells 题目链接:https://codeforces.com/gym/101194/attachments Input file: Standard Input ...

  8. LintCode之合并排序数组II

    题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...

  9. scrapy基础笔记

    公众号原文 公众号排版更友好,建议查看公众号原文 前言 reference: https://www.tutorialspoint.com/scrapy/scrapy_quick_guide.htm ...

  10. SHELL脚本里执行的东西需要多次回车确认,怎么实现自动回车确认?

    写了个自动配置的shell脚本,其中有几行是 …… ./build-key-server ./build-key-client …… 在执行build-key-server和build-key-cli ...