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】1035. Uncrossed Lines
题目如下: We write the integers of A and B (in the order they are given) on two separate horizontal line ...
- iOS---如何截图,如何将图片保存到相册
最近的项目中运用到了这两个功能,所以记录一下.做了一个小工程将两个方法结合到了一起 1 - (void)viewDidLoad { [super viewDidLoad]; UIButton * bt ...
- CentOS下安装Chrome浏览器中文显示为方框
执行如下三条命令 yum groupinstall "X Window System" -y yum -y groupinstall chinese-support yum -y ...
- 使用 pyenv 管理 Python 版本
http://einverne.github.io/post/2017/04/pyenv.html Posted on 04/22/2017 by Ein Verne | View revisio ...
- ajax传递对象到MVC控制器
1.view层中ajax写法: function Add2() { var model = new Object(); model.UserName = $('#UserName').val(); m ...
- php对象转数组的函数
关于php中想让对象以数组的形式访问,这时候就需要使用到get_object_vars()函数了.先来介绍一下这个函数. 官方文档是这样解释的: 1 array get_object_vars ( o ...
- Java 设计模式之 简单工厂模式(静态工厂方法模式)
简单工厂模式(Simple Factory Pattern)属于类的创新型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern),是通过专门定义一个类来负责创建其他类的 ...
- ThreadPoolExecutor实现异步多线程
import time from concurrent.futures import ThreadPoolExecutor executor = ThreadPoolExecutor(max_work ...
- MySql常用语句总结更新
1.Mysql修改字段的默认值: alter table tablename alter column drop default; (若本身存在默认值,则先删除) alter table tablen ...
- Log4j log for java(java的日志) 的使用
log4j的使用,Log4j log for java(java的日志) 是java主流的日志框架,提供各种类型,各种存储,各种格式,多样化的日志服务. 可以再Apache官网下载得到. 我们下载lo ...