当前位置:网站首页>力扣解法汇总1200-最小绝对差
力扣解法汇总1200-最小绝对差
2022-07-05 16:55:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你个整数数组 arr,其中每个元素都 不相同。
请你找到所有具有最小绝对差的元素对,并且按升序的顺序返回。
示例 1:
输入:arr = [4,2,1,3]
输出:[[1,2],[2,3],[3,4]]
示例 2:
输入:arr = [1,3,6,10,15]
输出:[[1,3]]
示例 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-absolute-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这题比较简单,排序之后,最小值一定是相邻的两者之间的最差值。
代码:
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;
}
}边栏推荐
- 33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
- Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
- Allusions of King Xuan of Qi Dynasty
- [Jianzhi offer] 66 Build product array
- The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
- Embedded-c Language-3
- Read the basic grammar of C language in one article
- Error in composer installation: no composer lock file present.
- VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
猜你喜欢

兰空图床苹果快捷指令

Practical example of propeller easydl: automatic scratch recognition of industrial parts
Example tutorial of SQL deduplication
深入理解Redis内存淘汰策略

Embedded-c Language-2
Summary of optimization scheme for implementing delay queue based on redis

Using C language to realize palindrome number

Rider 设置选中单词侧边高亮,去除警告建议高亮

Winedt common shortcut key modify shortcut key latex compile button

Copy mode DMA
随机推荐
C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
WebApp开发-Google官方教程
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
CMake教程Step3(添加库的使用要求)
In depth understanding of redis memory obsolescence strategy
关于mysql中的json解析函数JSON_EXTRACT
Machine learning 02: model evaluation
Embedded-c Language-1
Copy mode DMA
CMake教程Step4(安装和测试)
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
Oracle缩表空间的完整解决实例
Using C language to realize palindrome number
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
easyNmon使用汇总
Complete solution instance of Oracle shrink table space
[Jianzhi offer] 66 Build product array
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)