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 ...
随机推荐
- 基于Ajax技术的前后端Json数据交互方式实现
前言 使用浏览器访问网站是日常生活中必不可少的一件事情,当我们在浏览器地址栏中输入网址后会看到网站的内容,那么这个过程中发生了什么?下面简单介绍下浏览器访问网站过程. 第一步:浏览器向DNS服务器发起 ...
- git命令行提交流程
一.顺利提交无冲突情况(diff->add->fetch->pull->commit->push) 1.git status 查看状态 2. git diff head ...
- bat文件设置环境变量脚本
:: 获取管理员权限 @echo off % mshta vbscript:CreateObject()(window.close)&&exit cd /d "%~dp0&q ...
- java学习第一天:环境的配置
1.下载JDK,当前版本下载地址为:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.htm ...
- 数据结构实验之链表三:链表的逆置(SDUT 2118)
题目链接 #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; ...
- Mybatis源码学习之资源加载(六)
类加载器简介 Java虚拟机中的类加载器(ClassLoader)负责加载来自文件系统.网络或其他来源的类文件.Java虚拟机中的类加载器默认使用的是双亲委派模式,如图所示,其中有三种默认使用的类加载 ...
- nuxt使用教程
1 引言 Nuxt 是基于 Vue 的前端开发框架,这次我们通过 Introduction toNuxtJS 视频了解框架特色以及前端开发框架的基本要素. nuxt 与 next 结构很像,可以结合在 ...
- C#操作 Access 2013(.accdb)的方法
使用的Microsoft.Jet.OLEDB.4.0,的方法并不能连接最新的Access 存储文件,而且Microsoft.Jet.OLEDB.4.0不能使用x64的方式生成,而且使用这个数据库引擎效 ...
- 2 - Rich feature hierarchies for accurate object detection and semantic segmentation(阅读翻译)
Rich feature hierarchies for accurate object detection and semantic segmentation Ross Girshick Jeff ...
- Leetcode题目155.最小栈(简单)
题目描述: 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶 ...