当前位置:网站首页>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;
}
}
边栏推荐
- Is it safe for qiniu business school to open a stock account? Is it reliable?
- 漫画:有趣的海盗问题 (完整版)
- MYSQL group by 有哪些注意事项
- WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
- MySql 查询符合条件的最新数据行
- Embedded-c Language-5
- Embedded -arm (bare board development) -1
- Mysql5.6 parsing JSON strings (supporting complex nested formats)
- 【性能测试】全链路压测
- [first lecture on robot coordinate system]
猜你喜欢
WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
Judge whether a number is a prime number (prime number)
[Web attack and Defense] WAF detection technology map
thinkphp3.2.3
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
[first lecture on robot coordinate system]
High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
First day of learning C language
深入理解Redis内存淘汰策略
随机推荐
Mysql5.6 parsing JSON strings (supporting complex nested formats)
Redis+caffeine two-level cache enables smooth access speed
Embedded UC (UNIX System Advanced Programming) -2
【beanshell】数据写入本地多种方法
兰空图床苹果快捷指令
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
What are the precautions for MySQL group by
一文了解Go语言中的函数与方法的用法
[Jianzhi offer] 61 Shunzi in playing cards
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?
Read the basic grammar of C language in one article
The second day of learning C language for Asian people
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
The third lesson of EasyX learning
mysql中取出json字段的小技巧
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
[Web attack and Defense] WAF detection technology map
这个17岁的黑客天才,破解了第一代iPhone!
What else do you not know about new map()