当前位置:网站首页>[1200. Minimum absolute difference]
[1200. Minimum absolute difference]
2022-07-04 20:57:00 【[email protected]】
source : Power button (LeetCode)
describe :
Here's an array of integers arr
, Each of these elements is inequality .
Please find all the elements with the least absolute difference , And return in ascending order .
Example 1:
Input :arr = [4,2,1,3]
Output :[[1,2],[2,3],[3,4]]
Example 2:
Input :arr = [1,3,6,10,15]
Output :[[1,3]]
Example 3:
Input :arr = [3,8,-10,23,19,-4,-14,27]
Output :[[-14,-10],[19,23],[23,27]]
Tips :
- 2 <= arr.length <= 105
- -106 <= arr[i] <= 106
Method : Sort + One traverse
Ideas and algorithms
Code :
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
int n = arr.size();
sort(arr.begin(), arr.end());
int best = INT_MAX;
vector<vector<int>> ans;
for (int i = 0; i < n - 1; ++i) {
if (int delta = arr[i + 1] - arr[i]; delta < best) {
best = delta;
ans = {
{
arr[i], arr[i + 1]}};
}
else if (delta == best) {
ans.emplace_back(initializer_list<int>{
arr[i], arr[i + 1]});
}
}
return ans;
}
};
Execution time :52 ms, In all C++ Defeated in submission 98.35% Users of
Memory consumption :31.3 MB, In all C++ Defeated in submission 86.32% Users of
author:LeetCode-Solution
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041936120104.html
边栏推荐
- How does wincc7.5 SP1 find variables and their positions through cross indexing?
- Jekins initialization password not found or not found
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
- Go notes (1) go language introduction and characteristics
- 【解决方案】PaddlePaddle 2.x调用静态图模式
- LeetCode 8. String conversion integer (ATOI)
- Practical examples of node strong cache and negotiation cache
- Idea configuration standard notes
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
猜你喜欢
idea恢复默认快捷键
How does win11 search for wireless displays? Win11 method of finding wireless display device
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
Managed service network: application architecture evolution in the cloud native Era
Automatic generation of interface automatic test cases by actual operation
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
AP8022开关电源小家电ACDC芯片离线式开关电源IC
LeetCode+ 81 - 85 单调栈专题
Practical examples of node strong cache and negotiation cache
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
随机推荐
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
卷积神经网络在深度学习中新发展的5篇论文推荐
What if the brightness of win11 is locked? Solution to win11 brightness locking
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
分析伦敦银走势图的技巧
Length of the longest integrable subarray
Function analysis and source code of hash guessing game system development
MySQL - database query - use of aggregate function, aggregate query, grouping query
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
剑指 Offer II 80-100(持续更新)
黄金k线图中的三角形有几种?
What if win11u disk refuses access? An effective solution to win11u disk access denial
Idea plug-in
LeetCode 7. Integer inversion
Advantages of semantic tags and block level inline elements
字节测试工程师十年经验直击UI 自动化测试痛点
The concept and application of hash table
Win11无法将值写入注册表项如何解决?
Play the music of youth
How to solve the problem that win11 cannot write the value to the registry key?