当前位置:网站首页>力扣今日题-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
边栏推荐
- Find numbers
- Hidden communication tunnel technology: intranet penetration tool NPS
- C language: implementation of daffodil number function
- 程序员怎么才能提高代码编写速度?
- 同构图与异构图CYPHER-TASK设计与TASK锁机制
- AI system content recommendation issue 24
- Stew in disorder
- Market trend report, technical innovation and market forecast of taillight components in China
- Expression #1 of ORDER BY clause is not in SELECT list, references column ‘d.dept_ no‘ which is not i
- Cut! 39 year old Ali P9, saved 150million
猜你喜欢
What is torch NN?
D3D11_ Chili_ Tutorial (2): draw a triangle
Preliminary practice of niuke.com (10)
Web components series - detailed slides
一图看懂ThreadLocal
Opencv learning -- geometric transformation of image processing
Understand asp Net core - Authentication Based on jwtbearer
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
随机推荐
c# 实现定义一套中间SQL可以跨库执行的SQL语句
[Previous line repeated 995 more times]RecursionError: maximum recursion depth exceeded
TypeError: list indices must be integers or slices, not str
C# 实现 FFT 正反变换 和 频域滤波
Sql实现Split
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
AI system content recommendation issue 24
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
Practice: fabric user certificate revocation operation process
Will the memory of ParticleSystem be affected by maxparticles
Preliminary practice of niuke.com (10)
Research Report on market supply and demand and strategy of China's plastics and polymer industry
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Find numbers
The vscode waveform curve prompts that the header file cannot be found (an error is reported if the header file exists)
程序员怎么才能提高代码编写速度?
Web components series - detailed slides
Expression #1 of ORDER BY clause is not in SELECT list, references column ‘d.dept_ no‘ which is not i
China's plastic processing machinery market trend report, technological innovation and market forecast
函數式接口,方法引用,Lambda實現的List集合排序小工具