当前位置:网站首页>力扣今日题-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
边栏推荐
- Hair and fuzz interceptor Industry Research Report - market status analysis and development prospect forecast
- Can I "reverse" a Boolean value- Can I 'invert' a bool?
- 函數式接口,方法引用,Lambda實現的List集合排序小工具
- Four point probe Industry Research Report - market status analysis and development prospect prediction
- Anta is actually a technology company? These operations fool netizens
- Book of night sky 53 "stone soup" of Apache open source community
- MFC implementation of ACM basic questions encoded by the number of characters
- D3D11_ Chili_ Tutorial (2): draw a triangle
- Game theory
- China tall oil fatty acid market trend report, technical dynamic innovation and market forecast
猜你喜欢
Filtered off site request to
A trap used by combinelatest and a debouncetime based solution
What is torch NN?
Principle and general steps of SQL injection
Vscode prompt Please install clang or check configuration 'clang executable‘
@EnableAspectAutoJAutoProxy_ Exposeproxy property
What is the catalog of SAP commerce cloud
Redis' optimistic lock and pessimistic lock for solving transaction conflicts
Move, say goodbye to the past again
Common knowledge of unity Editor Extension
随机推荐
AI system content recommendation issue 24
Digital recognition system based on OpenCV
China tall oil fatty acid market trend report, technical dynamic innovation and market forecast
Some fields of the crawler that should be output in Chinese are output as none
Overview of convolutional neural network structure optimization
Socks agent tools earthworm, ssoks
MFC implementation of ACM basic questions encoded by the number of characters
Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
Principle and general steps of SQL injection
The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
Intranet penetrating FRP: hidden communication tunnel technology
Working group and domain analysis of Intranet
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Rearrange array
Accounting regulations and professional ethics [9]
Hidden communication tunnel technology: intranet penetration tool NPS
Accounting regulations and professional ethics [6]
Accounting regulations and professional ethics [11]
JS to realize the countdown function
[book club issue 13] packaging format and coding format of audio files