这是小川的第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中的最小绝对差值,通过排序来完成,借助Arrayssort方法实现,因为绝对值差最小的两个数肯定是相邻越近越小。第二,再次遍历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)的更多相关文章

  1. [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  2. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  3. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  4. 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

  5. C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...

  6. 【leetcode】1200. Minimum Absolute Difference

    题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute ...

  7. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  8. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 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 ...

随机推荐

  1. SpringDatajpa 使用原生的SQL进行分组查询

    话不多说,直接上代码 dao nativeQuery = true ---> 执行原生的SQL语法,也就是说这段sql拷贝到数据库中,然后就运行. 我们期望的结果: 取值: 取值结果: 结合实际 ...

  2. [Spring] Spring Data JPA

    Previouly we need to define a DAO interface and a DAO impl for 'Employee', it is not so reuseable, s ...

  3. Redis:RedisHelper(5)

    /// <summary> /// Redis 助手 /// </summary> public class RedisHelper { /// <summary> ...

  4. 菜鸟刷面试题(五、Java容器篇)

    目录: java 容器都有哪些? Collection 和 Collections 有什么区别? List.Set.Map 之间的区别是什么? HashMap 和 Hashtable 有什么区别? 如 ...

  5. Unable to copy file, Access to the path is denied

    Unable to copy file, Access to the path is denied http://stackoverflow.com/questions/7130136/unable- ...

  6. Connect AS400 through firewall(JDBC will require ports: 449, 8470, 8471, and 8476)

    What TCP ports are used by ODBC to connect to the DB2/400?  8471/9471 http://search400.techtarget.co ...

  7. 前端vue的get和post请求

    vue的get和post需要两个文件vue.js和vue-resource.js 以下是实现的代码,可以参考一下,需要注意的接口的请求需要考虑跨域的问题,其次就是访问页面需要在tomcat下访问,否则 ...

  8. codeforces723E

    One-Way Reform CodeForces - 723E There are n cities and m two-way roads in Berland, each road connec ...

  9. Python实用黑科技——解包元素(2)

    需求: 前面的文章讲的是使用变量的个数需要和迭代器数据变量的元素个数相同的方法,但更多的时候确实不想根据元素个数n来定义相应多的变量,而是希望用较少的变量( def drop_first_last(g ...

  10. CF1204A

    CF1204A. BowWow and the Timetable 题意: 给你一个2进制数,求这个2进制数在10进制中的 $ 4^i $ 的个数. 解法: 其实就是 $ \ulcorner_{\lo ...