【leetcode】1200. Minimum Absolute Difference
题目如下:
Given an array of distinct integers
arr, find all pairs of elements with the minimum absolute difference of any two elements.Return a list of pairs in ascending order(with respect to pairs), each pair
[a, b]follows
a, bare fromarra < bb - aequals to the minimum absolute difference of any two elements inarrExample 1:
Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.Example 2:
Input: arr = [1,3,6,10,15]
Output: [[1,3]]Example 3:
Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]Constraints:
2 <= arr.length <= 10^5-10^6 <= arr[i] <= 10^6
解题思路:把arr排好序后依次求相邻两个元素的差值,即可得到结果。
代码如下:
class Solution(object):
def minimumAbsDifference(self, arr):
"""
:type arr: List[int]
:rtype: List[List[int]]
"""
arr.sort()
res = []
min_diff = float('inf')
for i in range(len(arr)-1):
if arr[i+1] - arr[i] < min_diff:
res = [[arr[i],arr[i+1]]]
min_diff = arr[i+1] - arr[i]
elif arr[i+1] - arr[i] == min_diff:
res.append([arr[i],arr[i+1]])
return res
【leetcode】1200. Minimum Absolute Difference的更多相关文章
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- 【easy】530. Minimum Absolute Difference in BST
找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
随机推荐
- CoverflowJS
coverflow是苹果首创的将多首歌曲的封面以3D界面的形式显示出来的方式 GitHub地址:[https://github.com/coverflowjs/coverflow] 下载地址:[htt ...
- BeautifulSoup解析豆瓣即将上映的电影信息
工欲善其事,必先利其器,我们首先得了解beautifulsoup的使用,这其实是一个比较简单的东西 BeautifulSoup的基本使用语法规则 .find() 使用示例 soup.find('a ...
- SSM的开发步骤分析
完整开发步骤 导包 spring的jar包 mybatis的jar包 mybatis-Spring的jar包 aop的依赖jar包 oracle等数据库连接的jar包 DataSource的jar包 ...
- Spark集群架构
集群架构 SparkContext底层调度模块 Spark集群架构细化
- 【嵌入式开发】Raspberry Pi 树莓派性能测试
Raspberry Pi 树莓派性能测试 目录: CPU Linpack基准测试 源码 编译/运行 结果 Whetstone/Dhrystone综合基准测试 源码 编译/运行 结果 OpenSSL安全 ...
- poj1011(DFS+剪枝)
题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...
- iOS模拟器发生了崩溃,去哪找Crash Log
iOS模拟器发生了崩溃,可以在如下地方找到崩溃日志: ~/Library/Logs/DiagnosticReports/
- C++笔记(0)——判定一个数字是否是素数
博主之前使用的编程语言是Python,但是这门语言的效率比较低(通常,不优化的情况下,但是即便如此我还是偏爱Python),而且博主打算参加PAT考试(真正的原因),及博主打算顺便深入学习下机器学习框 ...
- tp5后台同步更新配置文件
thinkphp5 配置文件路径:app/extra/web.php public function add(){ $path = 'app/extra/web.php'; $file = inclu ...
- HNUSTOJ-1674 水果消除(搜索或并查集)
1674: 水果消除 时间限制: 2 Sec 内存限制: 128 MB提交: 335 解决: 164[提交][状态][讨论版] 题目描述 “水果消除”是一款手机游戏,相信大家都玩过或玩过类似的游戏 ...