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. 光流(optical flow)和openCV中实现

    转载请注明出处! ! ! http://blog.csdn.net/zhonghuan1992 光流(optical flow)和openCV中实现 光流的概念:        是Gibson在195 ...

  2. 基于PHP函数的alert弹框

    可以设置弹出信息,跳转地址,跳转的时间,跳转的信息标题提示: 手机端加上<meta name='viewport' content='width=device-width, initial-sc ...

  3. python day - 09 函数

    函数 1.函数的定义,引用. 定义:函数是对功能和代码块的封装和定义. 函数用 def关键字来表示. 格式: def  函数名(): 函数体 eg: return(返回值) 在函数中遇到return ...

  4. Latex 5: LaTeX资料下载

    转: LaTeX资料下载 最全latex资料下载   LaTeX命令速查手册1  

  5. vue开发总结(一)

    vue使用快一个多月了,从移动端到PC端,踩过的坑也不少.项目的开发是基于element ui 与 mint ui 组件库,下面总结下项目中的一些知识点: 一.网页数据请求 首次进入页面,需要请求数据 ...

  6. UVA11624 Fire! —— BFS

    题目链接:https://vjudge.net/problem/UVA-11624 题解: 坑点:“portions of the maze havecaught on fire”, 表明了起火点不唯 ...

  7. “There's no Qt version assigned to this project for platform ” - visual studio plugin for Qt

    1.find menu "Qt VS Tools", select Qt Options 2.add a new Qt version 3. right click the tar ...

  8. chan

    第一单元:分型.笔.线段 ?1  分型 一.分型.笔和线段所属范畴 缠师在<教你炒股票72:本ID已有课程的再梳理>中对缠论做过这样的说明“本ID的理论,本质上分两部分,一是形态学,二是动 ...

  9. codeforces 669D D. Little Artem and Dance(乱搞题)

    题目链接: D. Little Artem and Dance time limit per test 2 seconds memory limit per test 256 megabytes in ...

  10. BZOJ1040:骑士(基环树DP)

    Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在和平环境中 ...