当前位置:网站首页>[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
边栏推荐
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- jekins初始化密码没有或找不到
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
- 实操自动生成接口自动化测试用例
- 电脑怎么保存网页到桌面上使用
- 分析伦敦银走势图的技巧
- Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
- Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
- What is the development of block hash quiz game system? Hash quiz game system development (case mature)
猜你喜欢

阿里测试师用UI自动化测试实现元素定位

Alibaba testers use UI automated testing to achieve element positioning

Ten years' experience of byte test engineer directly hits the pain point of UI automation test

实操自动生成接口自动化测试用例

6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库

电脑怎么保存网页到桌面上使用

Jiuqi ny8b062d MCU specification /datasheet

How does wincc7.5 SP1 find variables and their positions through cross indexing?

Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)

分析伦敦银走势图的技巧
随机推荐
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Sword finger offer II 80-100 (continuous update)
语义化标签的优势和块级行内元素
How to adapt your games to different sizes of mobile screen
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
uniapp 富文本编辑器使用
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
记一次重复造轮子(Obsidian 插件设置说明汉化)
记录线上bug解决list(未完待续7/4)
栈:如何实现有效括号的判断?
Browser render page pass
Pytorch---使用Pytorch实现LinkNet进行语义分割
What should I do if my computer sharing printer refuses access
Some suggestions for interface design
idea配置标准注释
Function analysis and source code of hash guessing game system development
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
