LeetCode.1200-最小绝对值差(Minimum Absolute Difference)
这是小川的第418次更新,第451篇原创
看题和准备
今天介绍的是LeetCode算法题中Easy级别的第268题(顺位题号是1200)。给定一个由不同的整数组成的数组arr,找到所有对元素,其中任意两个元素的绝对差值都最小。
以升序返回关于配对的列表(相对于配对),每对[a,b]紧随其后:
- a,b来自
arr - a < b
b-a等于arr中任何两个元素的最小绝对差
例如:
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]
说明:最小绝对差为1。以升序列出所有差等于1的对。
输入:arr = [1,3,6,10,15]
输出:[[1,3]]
输入:arr = [3,8,-10,23,19,-4,-14,27]
输出:[[-14,-10],[19,23],[23,27]]
限制条件:
- 2 <=
arr.length<= 10^5 - -10^6 <=
arr[i]<= 10^6
第一种解法
直接翻译题目即可,分两步走,第一,找到数组arr中的最小绝对差值,通过排序来完成,借助Arrays的sort方法实现,因为绝对值差最小的两个数肯定是相邻越近越小。第二,再次遍历arr数组,将绝对值差等于最小绝对值差的两个元素添加到结果list中去。
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
int min = Integer.MAX_VALUE, len = arr.length;
for (int i=1; i<len; i++) {
min = Math.min(min, arr[i]-arr[i-1]);
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i=1; i<len; i++) {
if (arr[i]-arr[i-1] == min) {
List<Integer> list = new ArrayList<Integer>();
list.add(arr[i-1]);
list.add(arr[i]);
result.add(list);
}
}
return result;
}
第二种解法
针对第一种解法,我们还可以简化下,只使用一个循环。在循环里判断最小绝对值差,如果当前两元素的绝对值小于最小绝对值差,则更新最小绝对值差的值,同时将结果list清空,这里清空list有两种办法,一是重新创建对象,二是使用clear方法,推荐第一种重新创建对象的做法,将两元素存入结果list中。
public List<List<Integer>> minimumAbsDifference2(int[] arr) {
Arrays.sort(arr);Arrays.sort(arr);
int min = Integer.MAX_VALUE, len = arr.length;
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i=1; i<len; i++) {
if (arr[i] - arr[i-1] <= min) {
if (arr[i] - arr[i-1] < min) {
min = arr[i] - arr[i-1];
result = new ArrayList<List<Integer>>();
}
result.add(Arrays.asList(arr[i-1], arr[i]));
}
}
return result;
}
小结
算法专题目前已更新LeetCode算法题文章274+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、在看就是对我最大的回报和支持!
LeetCode.1200-最小绝对值差(Minimum Absolute Difference)的更多相关文章
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...
- 【leetcode】1200. Minimum Absolute Difference
题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 51. leetcode 530. Minimum Absolute Difference in BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
随机推荐
- 3-cmd命令
1.查看IPC$是否启用 命令:net share 2.启动/停止windows服务 命令:net start MSDTC net stop MSDTC 3.修改服务的启动类型(start= ...
- stm32——modbus例程网址收藏
https://blog.csdn.net/baidu_31437863/article/details/82178708 STM32(五) Modbus https://blog.csdn.net/ ...
- 【python爬虫】 爬云音乐我和xxx共同听过的歌曲
闲聊的时候,觉得,想写个爬虫,爬下2个人共同听过的歌曲有哪些,然后一鼓作气,花了一个多小时,写了一个.支持最近一周和所有时间,需要用户没有关闭听歌排行显示 How to start 使用到的工具是Se ...
- Lighting Techinology of the Last Of Us (2013 SIGGRAPH)
Lighting Techinology of the Last Of Us(2013 SIGGRAPH) or "Old Lightmaps - New Tricks" 原作:M ...
- 使用EntityFramework6连接MySql数据库-db first方式
准备工具: VS2013.MySQL For VisualStudio 1.1.4.Connector/Net 6.8.3 程序包管理器执行命令: Install-Package EntityFram ...
- Git始终忽略特定文件的某一行内容
笔者在编写Z Shell文件的时候经常会使用到set -x来开启调试,但不希望提交到仓库 解决方案 Git提供了一种文件过滤器,此方法可以帮助在提交时移除set -x 我们先来编写脚本,如何移除这一行 ...
- hierarchyviewer
支持的版本更低
- Web大文件分片上传
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- spring boot打包以及centos下部署
spring boot打包以及部署 一.打包 springboot的打包方式有很多种.有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的.这里主要介绍如何打成j ...
- BOM问题
在php编写中,很多人喜欢用notepad editplus 等等在windows下编写程序, 这就很容易出现一个问题,那就是文件签名的东西--BOM!所谓BOM,全称是Byte Order Mark ...