当前位置:网站首页>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
边栏推荐
- ~89 deformation translation
- Web components series - detailed slides
- Opencv learning -- arithmetic operation of image of basic operation
- CMPSC311 Linear Device
- Communication mode based on stm32f1 single chip microcomputer
- AutoCAD - set color
- Recommend 10 excellent mongodb GUI tools
- Will the memory of ParticleSystem be affected by maxparticles
- APOC custom functions and procedures
- TypeError: not enough arguments for format string
猜你喜欢
Position encoding practice in transformer
Function test - knowledge points and common interview questions
Software Engineer vs Hardware Engineer
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Opencv learning -- geometric transformation of image processing
Filtered off site request to
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
~88 running people practice
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
Model fusion -- stacking principle and Implementation
随机推荐
DIY a low-cost multi-functional dot matrix clock!
Statistical learning: logistic regression and cross entropy loss (pytoch Implementation)
Four point probe Industry Research Report - market status analysis and development prospect prediction
Object.keys()的用法
Lv166 turned over
时钟轮在 RPC 中的应用
Research Report on market supply and demand and strategy of China's well completion equipment industry
实战:fabric 用户证书吊销操作流程
Explore mongodb - mongodb compass installation, configuration and usage introduction | mongodb GUI
Inside and outside: flow chart drawing elementary: six common mistakes
One question per day 540 A single element in an ordered array
Filtered off site request to
Change the mouse pointer on ngclick - change the mouse pointer on ngclick
Accounting regulations and professional ethics [10]
Firebird experience summary
跳跃表实例
.Net 应用考虑x64生成
Research Report on surgical otorhinolaryngology equipment industry - market status analysis and development prospect prediction
China Indonesia adhesive market trend report, technological innovation and market forecast
Interpretation of the champion scheme of CVPR 2020 night target detection challenge