当前位置:网站首页>[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
边栏推荐
- Stack: how to realize the judgment of valid brackets?
- Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
- Advantages of semantic tags and block level inline elements
- 强化学习-学习笔记2 | 价值学习
- Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
- How does the computer save web pages to the desktop for use
- 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
- Leetcode+ 81 - 85 monotone stack topic
- 长城证券开户安全吗 股票开户流程网上开户
- 分析伦敦银走势图的技巧
猜你喜欢
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
托管式服务网络:云原生时代的应用体系架构进化
How does win11 search for wireless displays? Win11 method of finding wireless display device
Some suggestions for interface design
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
See how Tencent does interface automation testing
黄金k线图中的三角形有几种?
Jiuqi ny8b062d MCU specification /datasheet
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
随机推荐
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
剑指 Offer II 80-100(持续更新)
vim异步问题
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
记录线上bug解决list(未完待续7/4)
go语言笔记(2)go一些简单运用
卷积神经网络在深度学习中新发展的5篇论文推荐
扩展你的KUBECTL功能
分析伦敦银走势图的技巧
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
idea恢复默认快捷键
Pytorch---使用Pytorch实现LinkNet进行语义分割
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
word中插入图片后,图片上方有一空行,且删除后布局变乱
RFID仓库管理系统解决方案有哪些功能模块
Form组件常用校验规则-1(持续更新中~)
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
黄金k线图中的三角形有几种?
How does the computer save web pages to the desktop for use
Some suggestions for interface design