当前位置:网站首页>LeetCode 1200. 最小绝对差
LeetCode 1200. 最小绝对差
2022-07-06 06:02:00 【Sasakihaise_】
【分析】排序后判断即可。
List和Set清空可以用clear或者直接重新new一个,二者效率相差无异。
但是需要注意定义一个较大值的时候最好用(int)1e9这种,不要用1<<30。
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> ans = new ArrayList();
int min = (int)1e7;
for (var i = 1; i < arr.length; i++) {
int t = arr[i] - arr[i - 1];
if (t < min) {
min = t;
ans.clear();
}
if (t == min) {
ArrayList<Integer> pair = new ArrayList();
pair.add(arr[i - 1]);
pair.add(arr[i]);
ans.add(pair);
}
}
return ans;
}
}
边栏推荐
- LeetCode 739. 每日温度
- Nodejs realizes the third-party login of Weibo
- IPv6 comprehensive experiment
- Network protocol model
- LeetCode 731. 我的日程安排表 II
- Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
- wib3.0 跨越,在跨越(ง •̀_•́)ง
- Sqlmap tutorial (III) practical skills II
- (5) Explanation of yolo-v3 core source code (3)
- [course notes] Compilation Principle
猜你喜欢
【无标题】
LAN communication process in the same network segment
About PHP startup, mongodb cannot find the specified module
Overview of three core areas of Mathematics: algebra
H3C防火墙RBM+VRRP 组网配置
Web service connector: Servlet
请求转发与重定向
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
(5) Explanation of yolo-v3 core source code (3)
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
随机推荐
Query the standard text code corresponding to a work center (s) in the production order
【Postman】Collections-运行配置之导入数据文件
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
[Thesis code] SML part code reading
Title 1093: character reverse order
HCIA复习
数学三大核心领域概述:几何
LeetCode 731. 我的日程安排表 II
【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
对数据安全的思考(转载)
nodejs实现微博第三方登录
H3C V7 switch configuration IRF
The usage and difference between strlen and sizeof
Bit operation rules
Reading notes of effective managers
Detailed explanation of BF and KMP
Dynamic programming -- knapsack problem
OSPF configuration command of Huawei equipment
Embedded point test of app
局域网同一个网段通信过程