当前位置:网站首页>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;
}
}
边栏推荐
- Overview of three core areas of Mathematics: geometry
- Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
- isam2运行流程
- 请求转发与重定向
- PAT(乙级)2022年夏季考试
- 对数据安全的思考(转载)
- Nodejs realizes the third-party login of Weibo
- 【Postman】Collections-运行配置之导入数据文件
- Hongliao Technology: how to quickly improve Tiktok store
- Configuring OSPF GR features for Huawei devices
猜你喜欢
The usage and difference between strlen and sizeof
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
【微信小程序】搭建开发工具环境
H3C V7版本交换机配置IRF
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
【无标题】
C language bubble sort
C language learning notes (mind map)
局域网同一个网段通信过程
随机推荐
LeetCode 731. 我的日程安排表 II
Bit operation rules
nodejs实现微博第三方登录
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
【Postman】Monitors 监测API可定时周期运行
Redis6 cluster setup
[leetcode] day96 - the first unique character & ransom letter & letter ectopic word
Usage of test macro of GTEST
Fault, error, failure of functional safety
H3C firewall rbm+vrrp networking configuration
Hongliao Technology: how to quickly improve Tiktok store
Hypothesis testing learning notes
【Postman】Collections-运行配置之导入数据文件
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
Web服务连接器:Servlet
[happy Spring Festival] if you feel happy, dance
《卓有成效的管理者》读书笔记
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
C language learning notes (mind map)