给定一个数组,和一个数字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. 使用pymysql操作数据库

    学习如何使用python的pymysql模块来操作mysql数据库 这里的基本用法主要借鉴了该篇博客:https://www.cnblogs.com/woider/p/5926744.html 因为这 ...

  2. (转)C# 单例模式

    文章1:  一.多线程不安全方式实现 public sealed class SingleInstance    {        private static SingleInstance inst ...

  3. HTML第六章总结

    Something Serious of HTML 这一章节主要讲了 HTML 的历史,还有如何使得 HTML 更加规范. HTML's Brief History 在 HTML4 之前,struct ...

  4. 雷林鹏分享:XML 语法规则

    XML 语法规则 XML 的语法规则很简单,且很有逻辑.这些规则很容易学习,也很容易使用. 所有的 XML 元素都必须有一个关闭标签 在 HTML 中,某些元素不必有一个关闭标签: This is a ...

  5. English trip M1 - AC1 My Dream Car Teacher:Corrine

    In this lesson you will learn to describe an object. 课上内容(Lesson) You want to rent a car. Go to the ...

  6. CentOS上安装seafile

    一.安装python2.7.14(CentOS7上python默认版本是Python 2.7.5 ,不需要安装)1.安装依赖包[root@web01 ~]# yum -y install zlib z ...

  7. 请问WCF 跟 WebService之间的相同跟异同

    https://social.msdn.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice W ...

  8. Java对MongoDB中的数据查询处理

    Java语言标准的数据库时MySQL,但是有些时候也会用到MongoDB,这次Boss交代处理MongoDB,所以讲代码以及思路记录下了 摸索的过程,才发现软件的适用还是很重要的啊!!! 我连接的Mo ...

  9. 【Java】【3】BeanUtils.copyProperties();将一个实体类的值复制到另外一个实体类

    正文: a,b为对象 BeanUtils.copyProperties(a,  b); 1,BeanUtils是org.springframework.beans.BeanUtils, a拷贝到b 2 ...

  10. PAT 1011 World Cup Betting

    1011 World Cup Betting (20 分)   With the 2010 FIFA World Cup running, football fans the world over w ...