当前位置:网站首页>力扣今日题-1200. 最小绝对差
力扣今日题-1200. 最小绝对差
2022-07-04 14:57:00 【抗争的小青年】
1200. 最小绝对差
难度:简单:happy:
思路:题目不是让求最小绝对值差吗?我们可以先按照升序进行排序,然后让下标从第二个值开始,依次去减前一个元素。并用一个值min
保存他们两个的差值,同时让它于我们之前设置的一个元素min_value
(永远保存最小值)进行比较,如果min=min_value
,添加进结果集res
。若碰到了跟小的差值min<min_value
,那就把之间的结果集清空res.clear()
,并再次将min
赋值给min_value
,同时,重新添加元素。然后输出结果集res
。
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
//先按照升序进行排序
Arrays.sort(arr);
//创建结果集
List<List<Integer>> res = new ArrayList<>();
//n用来保存arr的长度
int n = arr.length;
//min_value用来保存最小值
int min_value = Integer.MAX_VALUE;
//遍历
for(int i = 1 ;i < n; i++){
//计算差值,并保存给diff
int min = arr[i]-arr[i-1];
if(min <= min_value){
if(min < min_value){
//调用list的clear方法,释放List集合
res.clear();
//把最小值赋值给min_value
min_value = min;
}
//说明都是最小差值,差值都一样类似案例一的情况
res.add(List.of(arr[i-1],arr[i]));
}
}
return res;
}
}
参考代码:2022/7/4
边栏推荐
- error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
- 嵌入式软件架构设计-函数调用
- Accounting regulations and professional ethics [9]
- Big God explains open source buff gain strategy live broadcast
- [book club issue 13] packaging format and coding format of audio files
- Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
- 如何为ONgDB核心项目源码做贡献
- Common knowledge of unity Editor Extension
- [North Asia data recovery] a database data recovery case where the partition where the database is located is unrecognized due to the RAID disk failure of HP DL380 server
- 2021 Google vulnerability reward program review
猜你喜欢
Cut! 39 year old Ali P9, saved 150million
程序员怎么才能提高代码编写速度?
[North Asia data recovery] a database data recovery case where the partition where the database is located is unrecognized due to the RAID disk failure of HP DL380 server
AutoCAD - set color
Unity animation day05
Big God explains open source buff gain strategy live broadcast
Opencv learning -- geometric transformation of image processing
Filtered off site request to
Book of night sky 53 "stone soup" of Apache open source community
~89 deformation translation
随机推荐
Hidden communication tunnel technology: intranet penetration tool NPS
Vscode setting outline shortcut keys to improve efficiency
Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
. Net delay queue
同构图与异构图CYPHER-TASK设计与TASK锁机制
如何为ONgDB核心项目源码做贡献
C# 实现 FFT 正反变换 和 频域滤波
Four point probe Industry Research Report - market status analysis and development prospect prediction
MFC implementation of ACM basic questions encoded by the number of characters
@EnableAspectAutoJAutoProxy_ Exposeproxy property
Market trend report, technical innovation and market forecast of taillight components in China
Common knowledge of unity Editor Extension
Variable cannot have type 'void'
How to decrypt worksheet protection password in Excel file
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
基于check-point实现图数据构建任务
Research Report on market supply and demand and strategy of surgical stapler industry in China
Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
Filtered off site request to
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast