leetcode 001
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的更多相关文章
- [leetCode][001] Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- [Leetcode][001] Two Sum (Java)
题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...
- LeetCode #001# Two Sum(js描述)
索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...
- Leetcode 001. 两数之和(扩展)
1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...
- 【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 ...
- two Sum ---- LeetCode 001
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode 001 Two Sun
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
随机推荐
- LeetCode--044--通配符匹配(java)*
给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完全匹配才算 ...
- xcode7 添加个人账户 is not on any development teams
XCODE7已经可以免费真机测试, 但添加个人账户后,显示 is not on any development teams , 解决办法: 点击 “-” 删除当前账户,退出XCODE重新打开再添加即可 ...
- 包装CGFloat和用NSNumber初始化的区别?
@(CGFloat)和[NSNumber numberWith:CGFloat]的区别?
- spring boot构建
1.新建Maven工程 1.File-->new-->project-->maven project 2.webapp 3.工程名称 k3 2.Maven 三个常用命令 选 项目右击 ...
- MapServer教程
https://mapserver.org/ MapServer是一个开放源代码平台,用于将空间数据和交互式地图应用程序发布到Web.由OSGEO批准的MapServer项目指导委员会(PSC)负责管 ...
- Only variables should be passed by reference
报错位置代码: $status->type = array_pop(explode('\\',$status->type)) (此处$status->type值原本是 APP\ ...
- kafka centos安装发送消费消息
1. 请先下载安装文件,java环境需提前安装,解压到指定目录:tar -zxvf kafka_2.11-2.3.1.tgz -C /root/soft/ 从官网下载文件,上传到centos虚拟机指定 ...
- Vue实例1
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Support Vector Machine(1):线性可分集的决策边界
与Logistuc Regression相比,SVM是一种优化的分类算法,其动机是寻找一个最佳的决策边界,使得从决策边界与各组数据之间存在margin,并且需要使各侧的margin最大化.比较容易理解 ...
- 关于static以及final关键字
Static关键字: 可以用来修饰类中的属性.类中的方法.以及具体的某一个类. 1.用于修饰属性: 则表示该属性属于整个类,不论有多少个对象实例,所有的实例共同拥有一个static静态的成员变量.该变 ...