当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
【性能测试】jmeter+Grafana+influxdb部署实战
激动人心!2022开放原子全球开源峰会报名火热开启!
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
干货!半监督预训练对话模型 SPACE
Database design in multi tenant mode
Example tutorial of SQL deduplication
Oracle缩表空间的完整解决实例
thinkphp模板的使用
基于51单片机的电子时钟设计
Deeply cultivate 5g, and smart core continues to promote 5g applications
随机推荐
Using C language to realize palindrome number
mysql中取出json字段的小技巧
What are the precautions for MySQL group by
thinkphp模板的使用
Matery主题自定义(一)黑夜模式
Embedded-c Language-2
机器学习01:绪论
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
[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
C how TCP restricts the access traffic of a single client
Example tutorial of SQL deduplication
【beanshell】数据写入本地多种方法
[Jianzhi offer] 66 Build product array
【二叉树】根到叶路径上的不足节点
Tips for extracting JSON fields from MySQL
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
一个满分的项目文档是如何书写的|得物技术
蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
[Web attack and Defense] WAF detection technology map