给定一个数组,和一个数字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的更多相关文章

  1. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  2. LeetCode python实现题解(持续更新)

    目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...

  3. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

  4. LeetCode Python 位操作 1

    Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...

  5. Python - 001 - 类与实例间属性的理解

    Python是个很灵活的语言,光看它的类和实例间属性的访问机制就可以看出这一点,不过这一点还真的不好理解,做了些测试之后我的理解是这样的: 实例在访问class属性时,先检索自己的names, 如果有 ...

  6. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  7. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  8. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  9. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

随机推荐

  1. SQL语句内做除法得出百分比

    保留两位小数点 SELECT ROUND(CAST(field1 AS DOUBLE)/field2, 2) * 100 FROM TB; 不保留 SELECT CAST(field1 AS FLOA ...

  2. English Voice of <<Just Give Me A Reason>>

    Right from the start, you were a thief,打从一开始,你就是个偷心贼You stole my heart and你偷走了我的心I your willing vict ...

  3. LeetCode--441--排列硬币

    问题描述: 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围 ...

  4. Axios的默认配置(碎片知识)API

    axios API axios(config) axios({ method: 'Post', url: '/user/123', data: { //略 } }) axios(url[, confi ...

  5. MyEclipse6.5的反编译插件的安装

    常用的几种反编译工具 1. JD-GUI[推荐] JD-GUI是属于Java Decompiler项目(JD项目)下个的图形化运行方式的反编译器.JD-Eclipse属于Java Decompiler ...

  6. 维护满足max(+ or -)min<=k的区间

    这是一种经典的单调栈+线段树的维护方法. 从左到右枚举右端点. 线段树维护每一个左端点的max(+ or -)min的值. 每次右端点移动的时候,把a[i]加入单调栈. 每弹栈一次,便在线段树上把对应 ...

  7. MySQL视图(view)

    一.基本概念 视图是一个虚拟表,是sql的查询结果,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据,在使用视图时动态生成.视图的数据变化会影响到基表,基表的数据变化也会影响到视图 ...

  8. project euler113

    project euler 113 对于1个数字,如果他数位不减或者不增称为bouncy number,比如1233,33210.统计1-10^100中的bouncy number   思路:分为两种 ...

  9. Educational Codeforces Round 2 E - Lomsat gelral

    题意:每个节点有个值,求每个节点子树众数和 题解:可线段树合并,维护每个数出现次数和最大出现次数,以及最大出现次数的数的和 //#pragma GCC optimize(2) //#pragma GC ...

  10. 【PowerDesigner】【2】将工具栏显示出来

    问题:我的软件一打开,没有工具栏 解决方案:Tools→Customize Menus and Tools→Palette→Close 参考文档: PowerDesigner如何将消失的工具栏显示出来 ...