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. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  2. 对promise的研究1

    通过看阮一峰老师的文章写出来的特此注明 1.Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大.它由社区最早提出和实现,ES6 将其 ...

  3. 18 StringBuilder类型有何作用

  4. c#Main()方法,java 是小写main

    main 方法,staitc 静态关键首字母大写,区分大小写,java 是main小写,返回值 ,vodi,int参数:可选static void Main(string[] args){ }

  5. [CF1081H]Palindromic Magic

    题意:给两个字符串\(a,b\),求出有多少种不同的字符串能通过从第一个串中取出一个回文串,从第二个串中取出一个回文串,按顺序拼接得到. 题解:证明?看官方题解吧 一些定义: 回文串拆分:\(s=ab ...

  6. 【CF1257E】The Contest【线段树】

    题意:给定三个序列abc,问最少操作几次使得满足a<b<c 题解:将三个序列合并起来,设cnt[i][1/2/3]表示前i个数有几个是来自序列1/2/3的. 枚举第一个序列要到i,此时对于 ...

  7. EXP-00091和IMP-00010报错

    原来导出表的库:database ORACLE_SID=TEST0221 版本: 11.2.0.4需要导进的库:database ORACLE_SID=PROD 版本:10.2.0.1因为字符集的问题 ...

  8. [CSP-S模拟测试]:Rectangle(模拟+树状数组)

    题目描述 平面上有$n$个点,第$i$个点的坐标为$X_i,Y_i$.对于其中的一个非空点集$S$,定义$f(S)$为一个最小矩形,满足:$\bullet$覆盖$S$中所有的点(在边界上也算覆盖):$ ...

  9. ThreadPoolExecutor实现异步多线程

    import time from concurrent.futures import ThreadPoolExecutor executor = ThreadPoolExecutor(max_work ...

  10. qinwoyige

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...