def two_sum(li, target):
for i in range(len(li)):
for j in range(i+1, len(li)):
if li[i] + li[j] == target:
return i, j def bin_search(li, val, low, high):
while low <= high: # 候选区有值
mid = (low + high) // 2
if li[mid] == val:
return mid
elif li[mid] > val:
high = mid - 1
else: # li[mid] < val
low = mid + 1
return -1 def two_sum_2(li, target):
for i in range(len(li)):
a = li[i]
b = target - a
j = bin_search(li, b, i+1, len(li)-1)
if j > 0:
return i, j def two_sum_4(li, target):
d = {}
for i, num in enumerate(li):
a = li[i]
b = target - a
if b in d:
return d[b], i
else:
d[a] = i # [1,2,3,4]

two_sum问题的更多相关文章

  1. leetcode刷题1:两数之和two_sum

    题目:(难度:Easy) 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, ...

  2. PHP算法练习1:两数之和

    leetcode地址:https://leetcode-cn.com/problems/two-sum/description/ 题目: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. ...

  3. python基础学习第五天

    li=[1,2,33,-1,'dbssd',[4,5,6],{4:'rfw',5:'re'}]del(li[1])print(li)print(type(li))#访问元素print(li[0])pr ...

  4. go语言中goroute使用:=遇到的坑

    先看下源代码,预想从1至N总取出所有能被a或b整除的正整数之和,为了利用go语言的并行优势,特使用goroute特性来实现,同时使用普通顺序计算进行效率比较分析 package chango impo ...

  5. 2sum,3sum,4sum,ksum

    1. 2sum 题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...

  6. LeetCode--Two_Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  7. 「LeetCode」0001-Two Sum(Ruby)

    题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...

  8. python 笔试总结

    1.对比两种函数对应结果 def fn(x): if x>0: print(x) fn(x-1) ****结果****** 3 2 1 $$$$$$另外一种$$$$$$$$$ def fn(x) ...

  9. C++学习五 const vector<int>类型的迭代器使用

    一情景: 算法功能:对于传入的vector, 能够找到两个数字,使其相加之和为target,然后返回这两个数字的位置(也就是秩) 最开始是这样的一个问题: 对于一个传入的const vector< ...

随机推荐

  1. 服务器返回JSON,IE出现下载问题

    我向来的观点,IE就是个奇葩. 服务器返回json,chrome处理得好地地,但IE却奇葩地向你请求是否要保存这个JSON文件? 之所以出现这种弱智现象,是因为IE无法识别一个所谓的响应头部:appl ...

  2. 使用TASM编译COFF格式和连接

    看到网络上流传的一份Drocon的mercury的代码程序源码使用TASM32编译使用MASM32来连接...关键的地方就在这里为什么要使用TASM编译...正常情况下TASM连接出来的程序代码体积远 ...

  3. elastica安装

    https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html

  4. Linux监控命令

    dd命令用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换.注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2它不是一个专业的测试工具,不过如果对于 ...

  5. jsp 中 jstl c:if等标签失效问题

    <c:if test="${page == 1}"> ${s.index+1} </c:if> <c:if test="${page > ...

  6. Mac JDK 多版本共存

    1.    安装各JDK版本,安装后通过Java -version检测是否安装好    2.    打开~/.bash_profile,没有的话创建    vim ~/.bash_profile   ...

  7. SpringBoot配置文件详解

    自定义属性与加载 com.dongk.selfproperty.title=wangdkcom.dongk.selfproperty.name=10000 然后通过@Value("${属性名 ...

  8. C3P0Tool

    c3p0-config.xml <c3p0-config> <named-config name="c3p0"> <property name=&qu ...

  9. MongoDb复制集实现故障转移,读写分离

    前言 数据库技术是信息系统的一个核心技术,发展很快,各种功能类型数据库层出不穷,之前工作中使用过关系型数据库(mysql.oracle等).面相对象数据库(db4o).key-value存储(Memc ...

  10. Gearman1.1.12安装与启动

    1)安装 a)安装gcc4.4环境: i.  yum install gcc44 gcc44-c++ libstdc++44-devel gcc-c++ -y ii. 在/etc/profile中添加 ...