当前位置:网站首页>力扣今日题-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
边栏推荐
- FIREBIRD使用经验总结
- Principle and general steps of SQL injection
- 嵌入式软件架构设计-函数调用
- [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
- Overview of convolutional neural network structure optimization
- Vscode setting outline shortcut keys to improve efficiency
- Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
- ~89 deformation translation
- Find numbers
- Research Report on market supply and demand and strategy of China's four sided flat bag industry
猜你喜欢
![[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](/img/79/3fab19045e1ab2f5163033afaa4309.jpg)
[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

What is torch NN?

error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up

165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version

Stress, anxiety or depression? Correct diagnosis and retreatment
![[hcie TAC] question 5 - 1](/img/e0/1b546de7628695ebed422ae57942e4.jpg)
[hcie TAC] question 5 - 1
![[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](/img/21/513042008483cf21fc66729ae1d41f.jpg)
[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

嵌入式软件架构设计-函数调用

Common knowledge of unity Editor Extension

科普达人丨一文看懂阿里云的秘密武器“神龙架构”
随机推荐
China Indonesia adhesive market trend report, technological innovation and market forecast
Filtered off site request to
Can I "reverse" a Boolean value- Can I 'invert' a bool?
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
Logstash ~ detailed explanation of logstash configuration (logstash.yml)
Market trend report, technical innovation and market forecast of China's hair repair therapeutic apparatus
C# 实现 FFT 正反变换 和 频域滤波
Accounting regulations and professional ethics [9]
Redis: SDS source code analysis
%F format character
std::shared_ ptr initialization: make_ shared&lt; Foo&gt; () vs shared_ ptr&lt; T&gt; (new Foo) [duplicate]
Model fusion -- stacking principle and Implementation
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
[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
Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
D3D11_ Chili_ Tutorial (2): draw a triangle
How can floating point numbers be compared with 0?
Cut! 39 year old Ali P9, saved 150million
CMPSC311 Linear Device
Research Report on market supply and demand and strategy of surgical stapler industry in China