当前位置:网站首页>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
边栏推荐
- Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda
- Research Report on plastic recycling machine industry - market status analysis and development prospect forecast
- Accounting regulations and professional ethics [6]
- C language: implementation of daffodil number function
- .Net 应用考虑x64生成
- DIY a low-cost multi-functional dot matrix clock!
- Game theory
- [Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
- Sql实现Split
- Salient map drawing based on OpenCV
猜你喜欢
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
AutoCAD - set color
Will the memory of ParticleSystem be affected by maxparticles
AI system content recommendation issue 24
Vscode prompt Please install clang or check configuration 'clang executable‘
TypeError: list indices must be integers or slices, not str
PR FAQ: how to set PR vertical screen sequence?
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Communication mode based on stm32f1 single chip microcomputer
Understand asp Net core - Authentication Based on jwtbearer
随机推荐
Expression #1 of ORDER BY clause is not in SELECT list, references column ‘d.dept_ no‘ which is not i
China's roof ladder market trend report, technological innovation and market forecast
Research Report on market supply and demand and strategy of surgical stapler industry in China
Model fusion -- stacking principle and Implementation
After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
SQL implements split
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
C language: implementation of daffodil number function
Hair growth shampoo industry Research Report - market status analysis and development prospect forecast
TypeError: not enough arguments for format string
Research Report on market supply and demand and strategy of China's four sided flat bag industry
Sql实现Split
The content of the source code crawled by the crawler is inconsistent with that in the developer mode
Accounting regulations and professional ethics [7]
Socks agent tools earthworm, ssoks
[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
Object.keys()的用法
Accounting regulations and professional ethics [11]
Preliminary practice of niuke.com (10)