当前位置:网站首页>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
边栏推荐
- Daily notes~
- AI system content recommendation issue 24
- ECCV 2022放榜了:1629篇论文中选,录用率不到20%
- FIREBIRD使用经验总结
- Cypher task design and task locking mechanism of isomorphic and heterogeneous graphs
- Position encoding practice in transformer
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- Model fusion -- stacking principle and Implementation
- Can I "reverse" a Boolean value- Can I 'invert' a bool?
- 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
The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Transformer中position encoding实践
Detailed process of DC-2 range construction and penetration practice (DC range Series)
TypeError: list indices must be integers or slices, not str
MFC implementation of ACM basic questions encoded by the number of characters
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
[Previous line repeated 995 more times]RecursionError: maximum recursion depth exceeded
随机推荐
Research Report on market supply and demand and strategy of China's Sodium Tetraphenylborate (cas+143-66-8) industry
表单传递时,如何隐式将值传过去
Use and principle of thread pool
Hair and fuzz interceptor Industry Research Report - market status analysis and development prospect forecast
Configuration instance of Oracle listener server and client
What is the catalog of SAP commerce cloud
Daily notes~
QT graphical view frame: element movement
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
@EnableAspectAutoJAutoProxy_ Exposeproxy property
After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
程序员怎么才能提高代码编写速度?
Understand the rate control mode rate control mode CBR, VBR, CRF (x264, x265, VPX)
Understand asp Net core - Authentication Based on jwtbearer
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
js中的数组筛选fliter
Model fusion -- stacking principle and Implementation
Inside and outside: flow chart drawing elementary: six common mistakes
Cut! 39 year old Ali P9, saved 150million
Interface test - knowledge points and common interview questions