当前位置:网站首页>Force deduction solution summary 1200 minimum absolute difference
Force deduction solution summary 1200 minimum absolute difference
2022-07-22 19:32:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Here's an array of integers arr, Each of these elements is inequality .
Please find all the elements with the least absolute difference , And return in ascending order .
Example 1:
Input :arr = [4,2,1,3]
Output :[[1,2],[2,3],[3,4]]
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]]
Tips :
2 <= arr.length <= 10^5
-10^6 <= arr[i] <= 10^6
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-absolute-difference
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * This question is relatively simple , After the sorting , The minimum value must be the worst difference between the adjacent two .
Code :
public class Solution1200 {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> result = new ArrayList<>();
int minDIff = Integer.MAX_VALUE;
for (int i = 1; i < arr.length; i++) {
int diffValue = arr[i] - arr[i - 1];
if (diffValue < minDIff) {
result.clear();
result.add(createList(arr[i], arr[i - 1]));
minDIff = diffValue;
continue;
}
if (diffValue == minDIff) {
result.add(createList(arr[i], arr[i - 1]));
continue;
}
}
return result;
}
private List<Integer> createList(int i1, int i2) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(i2);
integers.add(i1);
return integers;
}
}
边栏推荐
- Future prospects and trends of IT outsourcing service industry in various fields
- 2022 centos8 Yum image installation & Alibaba cloud MySQL 5.7 tutorial and problem solving
- arguments
- JS advanced - understanding of functions
- Leetcode daily question 2022/1/24-2022/1/30
- About human resource outsourcing companies
- Has the climate changed in Hebei and Zhejiang?
- Grafana panel - about conversion
- LeetCode 每日一题 2022/3/7-2022/3/13
- 软件产业未来发展的几个趋势
猜你喜欢
PHP implementation deletes a value in a one-dimensional array
Constructor
Nacos persistent connection MySQL database SM4 encryption scheme
Introduction to arrays
CentOS7安装Mysql5.7解压版&Navicat连接Mysql&防火墙设置——亲测有效
STM32中什么是预分频
Matlab plot子图的间距和边缘距离如何调整(已解决)
[FatFs] porting FatFs file system based on STM32 SD card
Laravel solves [1045] access denied for user 'homestead' @ 'localhost' (usin g password: yes)
Behind the explosion of collaborative office market: cloud communication capability is the focus of demand
随机推荐
Jackson 解析 JSON 详细教程
Has the climate changed in Hebei and Zhejiang?
Force deduction solution summary 1051 height checker
A practical example of the go pipeline pattern -- calculating the MD5 value of a series of files
About human resource outsourcing companies
LeetCode 每日一题 2022/1/17-2022/1/23
Fluent adjusts the drawing shape by dragging
Atmospheric environment monitoring scheme for 4G industrial router
Leetcode daily question 2022/2/21-2022/2/27
JS advanced - basic data type and reference data type
LeetCode 每日一题 2022/2/14-2022/2/20
Rongyun handles political affairs: "small grid" can also achieve "big governance"
Grafana panel - modify visual text and background colors
关于红队方面的学习资料
为什么重写equels方法一定要重写hashCode方法
融云漫话:没有一个人躲得过“视频会议”
nacos持久化连接mysql数据库sm4加密方案
Swagger-UI介绍及常用注解说明
Why is it necessary to rewrite the hashcode method when rewriting the requests method
Force deduction solution summary 1108-ip address invalidation