[lintcode the-smallest-difference]最小差(python)
题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/
给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|)。返回最小差。
排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值。并且依据他们两个数字的大小来决定谁来移动指针。
class Solution:
# @param A, B: Two lists of integer
# @return: An integer
def smallestDifference(self, A, B):
# write your code here
A.sort()
B.sort()
i = 0
j = 0
ret = 2147483647
while i < len(A) and j < len(B):
ret = min(ret, abs(A[i]-B[j]))
if A[i] > B[j]:
j += 1
elif A[i] < B[j]:
i += 1
elif A[i] == B[j]:
ret = 0
break
return ret
[lintcode the-smallest-difference]最小差(python)的更多相关文章
- LintCode 387: Smallest Difference
LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- Smallest Difference(POJ 2718)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6740 Accepted: 18 ...
- The Smallest Difference
Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...
- Smallest Difference(暴力全排列)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10387 Accepted: 2 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- 【POJ - 2718】Smallest Difference(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
随机推荐
- color mask
https://msdn.microsoft.com/zh-cn/library/windows/desktop/bb173595(v=vs.85).aspx void OMSetBlendState ...
- 索引服务混战ASP.NET――微软的又一个隔离墩
话说月初笔者在华山之巅搞定了ASP.NET一起莫名奇妙的异常,自此之后和公主过着…嘟--,不好意思,书都看杂了,串了台了.好,咱们闲言少叙,书归正传. 自从上次解决了由调试文件库引起的ASP.NET执 ...
- Sqli-labs less 34
Less-34 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理.由上面的例子可以看到我们的方法就是将过滤函数添加的 \ 给吃掉.而get型的方式我们是以url形式提交 ...
- rm删除命令
linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除了链接,原有 ...
- 教你如何利用xml格式的sitemap文件做好SEO
教你如何利用xml格式的sitemap文件做好SEO 浏览: | 更新:-- : 一般的网站中都有网站地图文件,它有HTML格式与XML格式,网站地图可以帮助搜索引擎抓取.帮助用户找到自己所需要的内容 ...
- Fbric、Ansible、Docker、Chaos Monkey:DevOps工具的年中回顾
Fbric.Ansible.Docker.Chaos Monkey:DevOps工具的年中回顾 [编者按]近日,Cyber Engineering Solutions Group 技术经理 Hasan ...
- TCP套接字编程
一.套接字(socket)函数 图1给出了在一个TCP客户与服务器通信的流程.服务器首先启动,稍后某个客户启动,它试图连接到服务器.假设客户给服务器发送一个请求,服务器处理该请求,并且给客户发回一个相 ...
- 项目中用到的SQL-总结
基本sql总结: Group by的理解:having子句,分组函数 Group by使用的限定: 1.出现在Select列表中的字段或者出现在order by后面的字段,如果不是包含在分组函数中,那 ...
- openvswitch 修改dpid(datapath id)
版本: $ sudo ovs-vsctl -Vovs-vsctl (Open vSwitch) 2.0.2Compiled May 13 2015 18:49:53 $ sudo ovs-vsctl ...
- Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验
Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...