当前位置:网站首页>力扣今日题-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
边栏推荐
- What is torch NN?
- Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
- D3D11_ Chili_ Tutorial (2): draw a triangle
- Game theory
- Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda
- [North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
- . Net delay queue
- What should ABAP do when it calls a third-party API and encounters garbled code?
- .Net 应用考虑x64生成
- 基于check-point实现图数据构建任务
猜你喜欢

MFC implementation of ACM basic questions encoded by the number of characters

Cut! 39 year old Ali P9, saved 150million

AutoCAD - set color

Ten clothing stores have nine losses. A little change will make you buy every day

Using celery in projects

~89 deformation translation
时钟轮在 RPC 中的应用

Web components series - detailed slides

Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda
![[native JS] optimized text rotation effect](/img/50/3c09f223e821c14e7e9e0fb47622b6.jpg)
[native JS] optimized text rotation effect
随机推荐
程序员怎么才能提高代码编写速度?
What encryption algorithm is used for the master password of odoo database?
[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server
[hcie TAC] question 5 - 1
Interpretation of the champion scheme of CVPR 2020 night target detection challenge
Explore mongodb - mongodb compass installation, configuration and usage introduction | mongodb GUI
Opencv learning -- arithmetic operation of image of basic operation
QT graphical view frame: element movement
Model fusion -- stacking principle and Implementation
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Preliminary practice of niuke.com (10)
One question per day 540 A single element in an ordered array
What is torch NN?
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
[book club issue 13] ffmpeg common methods for viewing media information and processing audio and video files
@EnableAspectAutoJAutoProxy_ Exposeproxy property
std::shared_ ptr initialization: make_ shared&lt; Foo&gt; () vs shared_ ptr&lt; T&gt; (new Foo) [duplicate]
Actual combat | use composite material 3 in application
Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
科普达人丨一文看懂阿里云的秘密武器“神龙架构”