leetcode python 001
给定一个数组,和一个数字target,要求返回和为target的两个数组成员的下标。
import numpy as np
import time
#### 构造题目 , x数组大小
x=100000
l1=np.random.rand(x)
l2=[round(l*x,5) for l in l1]
w1=np.random.randint(0,x)
w2=np.random.randint(0,x)
while w1==w2:
w2=np.random.randint(0,x)
print('答案%s,%s'%(w1,w2))
target=l2[w1]+l2[w2]
print('target %s'%target)
#### 开始计算
t=time.time()
d={}
for i in range(x):
if l2[i] in d.keys():
print(d[l2[i]],i)
print(l2[d[l2[i]]],l2[i])
break
d[target-l2[i]]=i
t=time.time()-t
print('%s 元素用时 %s s'%(x,t))
leetcode python 001的更多相关文章
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
- LeetCode Python 位操作 1
Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...
- Python - 001 - 类与实例间属性的理解
Python是个很灵活的语言,光看它的类和实例间属性的访问机制就可以看出这一点,不过这一点还真的不好理解,做了些测试之后我的理解是这样的: 实例在访问class属性时,先检索自己的names, 如果有 ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
随机推荐
- Java访问Redis
Redis的数据类型总共有如下几种 1.String(字符串) 2.List(列表),字符串列表,有序 3.Hash(哈希),可以存储类似于数据库的表结构 4.Set(集合),无序,不可重复 5.ZS ...
- for...in和for...of循环的区别
使用for...in和for...of分别对Array,Set,Map做测试 var a=["A","B","C"]; var b=new ...
- Centos6.8 smokeping安装
yum -y install rrdtool perl-rrdtool curl perl-core bind bind-chroot bind-utils httpd popt popt-devel ...
- LeetCode--231--2的幂函
问题描述: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 2 0 = 1 示例 2: 输入: 16 输出: true 解释: 2 4 ...
- BASE64图片转字符串
Java代码图片字符串互转 /** * 将base64字符串转成图片 * TODO * @param imgStr base64图片字符串 * @param path 目标输出路径 * @return ...
- 4.1.2 A Funny Game(POJ 2484)
Problem description: n枚硬币排成一个圈,A和B轮流从中取一枚或两枚硬币,不过取两枚时,所取的两枚硬币必须是连续的.硬币取走之后留下空位,相隔空位的硬币视为不连续的.A开始先取,取 ...
- Git创建新项目
1. git init 2. git remote add origin 3. git pull origin --allow-unrelated-histories 4. git push orig ...
- Remove Element leetcode java
问题描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 『Python』skimage图像处理_旋转图像
一段简短的实现图像旋转的代码,使用了skimage库,据说和PIL相比,skimage对numpy等科学计算库的支持更好,这里是为了完成师兄给的帮他修改程序的任务,如果以后有需求的话可能会对pytho ...
- python-django rest framework框架之序列化
序列化与反序列化: 对象 -> 字符串 序列化 字符串 -> 对象 反序列化 rest framework序列化+Form 目的: 解决QuerySet序列化问题 功能:解析 和 过滤 - ...