当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
Database: ODBC remote access SQL Server2008 in oracel
H3C firewall rbm+vrrp networking configuration
进程和线程的理解
养了只小猫咪
Clock in during winter vacation
Hongliao Technology: how to quickly improve Tiktok store
Nodejs realizes the third-party login of Weibo
Fault, error, failure of functional safety
【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
【微信小程序】搭建开发工具环境
随机推荐
Report on the competition status and investment decision recommendations of Guangxi hospital industry in China from 2022 to 2028
As3013 fire endurance test of cable distribution system
Linux regularly backs up MySQL database
Database: ODBC remote access SQL Server2008 in oracel
[C language syntax] the difference between typedef struct and struct
关于 PHP 启动 MongoDb 找不到指定模块问题
Usage of test macro of GTEST
Hypothesis testing learning notes
Application of Lie group in gtsam
【无标题】
[web security] nodejs prototype chain pollution analysis
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
[happy Spring Festival] if you feel happy, dance
【Postman】Monitors 监测API可定时周期运行
Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
Idea new UI usage
[ram IP] introduction and experiment of ram IP core
IPv6 comprehensive experiment
Introduction to promql of # yyds dry goods inventory # Prometheus
Function of activation function