当前位置:网站首页>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
边栏推荐
- PR FAQ: how to set PR vertical screen sequence?
- Model fusion -- stacking principle and Implementation
- Laravel simply realizes Alibaba cloud storage + Baidu AI Cloud image review
- Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
- 跳跃表实例
- [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
- Proxifier global agent software, which provides cross platform port forwarding and agent functions
- AI system content recommendation issue 24
- Feature extraction and detection 15-akaze local matching
- How to decrypt worksheet protection password in Excel file
猜你喜欢

Preliminary practice of niuke.com (10)

Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

从数数开始

Vscode setting outline shortcut keys to improve efficiency
Application of clock wheel in RPC

The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release

Intranet penetrating FRP: hidden communication tunnel technology

Hidden communication tunnel technology: intranet penetration tool NPS

程序员怎么才能提高代码编写速度?
随机推荐
Change the mouse pointer on ngclick - change the mouse pointer on ngclick
How to decrypt worksheet protection password in Excel file
Research Report on surgical otorhinolaryngology equipment industry - market status analysis and development prospect prediction
Web components series - detailed slides
Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
Firebird experience summary
What does IOT engineering learn and work for?
AutoCAD - set color
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
函數式接口,方法引用,Lambda實現的List集合排序小工具
Accounting regulations and professional ethics [9]
Explore mongodb - mongodb compass installation, configuration and usage introduction | mongodb GUI
[Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
Detailed process of DC-2 range construction and penetration practice (DC range Series)
Actual combat | use composite material 3 in application
Laravel simply realizes Alibaba cloud storage + Baidu AI Cloud image review
Move, say goodbye to the past again
Model fusion -- stacking principle and Implementation
Principle and general steps of SQL injection
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)