当前位置:网站首页>Force deduction solution summary 1200 minimum absolute difference
Force deduction solution summary 1200 minimum absolute difference
2022-07-05 17:26: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;
}
}边栏推荐
猜你喜欢

Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city

Three traversal methods of binary tree

CMake教程Step2(添加库)

基于51单片机的电子时钟设计

机器学习01:绪论

High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body

7.Scala类
深入理解Redis内存淘汰策略

Deeply cultivate 5g, and smart core continues to promote 5g applications

URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
随机推荐
一文了解Go语言中的函数与方法的用法
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
Rider 设置选中单词侧边高亮,去除警告建议高亮
Embedded -arm (bare board development) -2
Is it safe for qiniu business school to open a stock account? Is it reliable?
编译libssh2报错找不到openssl
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
[7.7 live broadcast preview] the lecturer of "typical architecture of SaaS cloud native applications" teaches you to easily build cloud native SaaS applications. Once the problem is solved, Huawei's s
Allusions of King Xuan of Qi Dynasty
Embedded UC (UNIX System Advanced Programming) -2
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
How MySQL uses JSON_ Extract() takes JSON value
Is it safe to open futures accounts online? Will there be more liars online? Doesn't feel very reliable?
IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
Zhang Ping'an: accélérer l'innovation numérique dans le cloud et construire conjointement un écosystème industriel intelligent
世界上最难的5种编程语言
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
張平安:加快雲上數字創新,共建產業智慧生態
一口气读懂 IT发展史