当前位置:网站首页>Li Kou today's question -1200 Minimum absolute difference
Li Kou today's question -1200 Minimum absolute difference
2022-07-04 16:45:00 【Struggling young man】
1200. Minimum absolute difference
difficulty : Simple :happy:
Ideas : The problem is not to find the minimum absolute value difference ? We can sort in ascending order first , Then let the subscript start with the second value , Subtract the previous element in turn . And use a value min
Save the difference between them , At the same time, let it be an element set before us min_value
( Always save the minimum ) Compare , If min=min_value
, Add to result set res
. If you encounter a small difference with min<min_value
, Then empty the result set between res.clear()
, And again min
Assign a value to min_value
, meanwhile , Re add elements . Then output the result set res
.
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
// First sort in ascending order
Arrays.sort(arr);
// Create result sets
List<List<Integer>> res = new ArrayList<>();
//n For preservation arr The length of
int n = arr.length;
//min_value Used to save the minimum value
int min_value = Integer.MAX_VALUE;
// Traverse
for(int i = 1 ;i < n; i++){
// Calculate the difference , And save it to diff
int min = arr[i]-arr[i-1];
if(min <= min_value){
if(min < min_value){
// call list Of clear Method , Release List aggregate
res.clear();
// Assign the minimum value to min_value
min_value = min;
}
// Description is the minimum difference , The difference is the same, similar to case 1
res.add(List.of(arr[i-1],arr[i]));
}
}
return res;
}
}
Reference code :2022/7/4
边栏推荐
- [Chongqing Guangdong education] National Open University spring 2019 1396 pharmaceutical administration and regulations (version) reference questions
- Configuration instance of Oracle listener server and client
- PR FAQ: how to set PR vertical screen sequence?
- Application and Optimization Practice of redis in vivo push platform
- Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- Change the mouse pointer on ngclick - change the mouse pointer on ngclick
- 散列表
- China Indonesia adhesive market trend report, technological innovation and market forecast
- Hair and fuzz interceptor Industry Research Report - market status analysis and development prospect forecast
猜你喜欢
Principle and general steps of SQL injection
Working group and domain analysis of Intranet
一图看懂ThreadLocal
Opencv learning -- geometric transformation of image processing
Common knowledge of unity Editor Extension
程序员怎么才能提高代码编写速度?
时钟轮在 RPC 中的应用
Interface test - knowledge points and common interview questions
Ten clothing stores have nine losses. A little change will make you buy every day
Application of clock wheel in RPC
随机推荐
Research Report of exoskeleton robot industry - market status analysis and development prospect prediction
Lv166 turned over
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
时钟轮在 RPC 中的应用
Essential basic knowledge of digital image processing
函數式接口,方法引用,Lambda實現的List集合排序小工具
Model fusion -- stacking principle and Implementation
Game theory
The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
MFC implementation of ACM basic questions encoded by the number of characters
Redis: SDS source code analysis
C language: implementation of daffodil number function
Vscode setting outline shortcut keys to improve efficiency
Digital recognition system based on OpenCV
基于check-point实现图数据构建任务
China's roof ladder market trend report, technological innovation and market 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
Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda
How to contribute to the source code of ongdb core project