【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 ...
随机推荐
- 在IOS系统中微信浏览器input输入框输入值无效
[contenteditable="true"], input, textarea { -webkit-user-select: auto!important; -khtml-us ...
- MySQL之视图学习
MYSQL---视图 1.概述: 视图是从一个或者多个表中导出的,视图的行为与表非常类似,但视图是一个虚拟表.在视图中用户可以使用SELECT语句查询数据,以及使用INSERT.UPDATE和DE ...
- 20191209 Linux就该这么学(1-3)
1. 部署虚拟环境安装 Linux 系统 RPM 是为了简化安装的复杂度,而 Yum软件仓库是为了解决软件包之间的依赖关系. 2. 新手必须掌握的Linux命令 通常来讲,计算机硬件是由运算器.控制器 ...
- AJAX得基本使用
直接上案例:
- Spring Boot常用功能
1.Spring Boot打war包配置 利用IDEA将SpringBoot的项目打包成war文件
- Spring的核心思想,总结得非常好!
依赖注入是面型接口编程的一种体现,是Spring的核心思想.事实上依赖注入并不是什么高深的技术, 只是被Sping这么以包装就显得有些神秘. 如上代码清单所示,Coder使用Java语言打印hello ...
- springboot基于CORS处理跨域问题
1. 为什么有跨域问题 跨域不一定都会有跨域问题. 因为跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是与当前页域名相同的路径,这能有效的阻止跨站攻击. 因此:跨域问 ...
- 解决iframe缓存机制导致页面不清除缓存不刷新页面的bug
在使用iframe时,已有页面嵌套了一个iframe页面,当这个页面提交后再次跳转到本页面时,原本iframe内的页面应该刷新数据的,结果未刷新,需要清除缓存后才刷新. 解决方案: var fresh ...
- JavaEE--JSP指令
JSP指令用于提供整个JSP页面的相关信息以及用于JSP页面与容器之间的通信.JSP指令有三种:page指令.include指令.taglib指令 1.page指令 用于设定整个JSP页面的属性和相关 ...
- 解决Asp.Net core 控制台出现乱码的情况
将控制台的编码页修改成Unicode,在运行程序或者在程序里加一行Console.OutputEncoding = Encoding.Unicode; Console.OutputEncoding = ...