【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, b
are fromarr
a < b
b - a
equals to the minimum absolute difference of any two elements inarr
Example 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 ...
随机推荐
- ARM的编程模式及寄存器
根据朱老师的课程及下面博客整理 http://blog.chinaunix.net/uid-20443992-id-5700979.html ARM 采用的是32位架构 ARM 约定: Byte : ...
- 小型自动化运维工具pssh和传输工具rsync
一.简单介绍 1.pssh全称是parallel-ssh,基于Python编写的并发在多台服务器上批量执行命令的工具.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括pssh ...
- javascript中,一个js中的函数,第一句var _this = this;为什么要这样做?
javascript中,一个js中的函数,第一句var _this = this;为什么要这样做? 下面是源码: 下面这段代码是常用的网站首页,自动切换span或者tabbar来变更List显示内容的 ...
- CM金丝雀Canary报错
参考: https://www.cnblogs.com/barneywill/p/10400788.html CM金丝雀Canary报错 1 HDFS 金丝雀Canary 测试无法为 /tmp/.cl ...
- Mycat+Mysql主从复制实现双机热备
Mycat+Mysql主从复制实现双机热备 一.mysql主从配置原理 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据 ...
- 克隆完虚拟机后修改网卡,修改静态IP
命令:vim /etc/sysconfg/network-scripts/ifcfg-eth0 编辑: vi /etc/udev/rules.d/70-persistent-net.rules 克隆 ...
- python-day25(正式学习)
目录 组合 多态 多态性 好处 封装 两个层面 property 组合 组合就是一个类的对象具备某一个属性,该属性的值是指向另外外一个类的对象 组合是用来解决类与类之间代码冗余的问题 首先我们先写一个 ...
- Python进阶编程 面向对象
一.面向对象 1.1面向对象的基本格式 class 类名: def 方法名(self): print(123) return 123 def 方法名(self): print(123) return ...
- 【MQ】为什么选择RocketMQ?
一.前言 提到mq,可能很多朋友都有多耳闻,很多大公司都在使用这种技术.就小编而言,听说使用mq可以进行秒杀的操作,而且使用十分的方便,效率十分的高.以前小编也做过关于秒杀的技术,就是使用悲观锁对DA ...
- Jade学习(五)之命令编译执行jade
首先全局安装jade,我们就可以使用jade 命令了! jade index.jade // 解析后会在文件夹中新生成一个压缩代码后的index.html 如果我们不想生成的index.html文件进 ...