当前位置:网站首页>Leetcode-1200: minimum absolute difference
Leetcode-1200: minimum absolute difference
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-1200: Minimum absolute difference
subject
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]]
Problem solving
Method 1 : Sort + One traverse
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
sort(arr.begin(),arr.end());
vector<vector<int>> res;
int minDistance=INT_MAX;
for(int i=1;i<arr.size();i++){
int distance=arr[i]-arr[i-1];
if(distance<minDistance){
minDistance=distance;
res.clear();
res.push_back({
arr[i-1],arr[i]});
}
else if(distance==minDistance){
res.push_back({
arr[i-1],arr[i]});
}
}
return res;
}
};
边栏推荐
- Appium foundation - use the first demo of appium
- How to adjust bugs in general projects ----- take you through the whole process by hand
- 【Rust 笔记】15-字符串与文本(上)
- 【Rust 笔记】14-集合(上)
- 1040 Longest Symmetric String
- 927. 三等分 模拟
- 1.13 - RISC/CISC
- Fried chicken nuggets and fifa22
- 网络工程师考核的一些常见的问题:WLAN、BGP、交换机
- 【Rust 笔记】13-迭代器(中)
猜你喜欢
数据可视化图表总结(一)
Appium自动化测试基础 — Appium测试环境搭建总结
In this indifferent world, light crying
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
EOJ 2021.10 E. XOR tree
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Solution to game 10 of the personal field
SQLMAP使用教程(二)实战技巧一
随机推荐
QT判断界面当前点击的按钮和当前鼠标坐标
传统数据库逐渐“难适应”,云原生数据库脱颖而出
QQ computer version cancels escape character input expression
Overview of variable resistors - structure, operation and different applications
One question per day 1447 Simplest fraction
liunx启动redis
Daily question 1984 Minimum difference in student scores
[practical skills] how to do a good job in technical training?
leetcode-9:回文数
可变电阻器概述——结构、工作和不同应用
【Rust 笔记】17-并发(上)
开源存储这么香,为何我们还要坚持自研?
js快速将json数据转换为url参数
【Rust 笔记】13-迭代器(下)
从Dijkstra的图灵奖演讲论科技创业者特点
One question per day 1765 The highest point in the map
【Rust 笔记】13-迭代器(中)
R language [import and export of dataset]
leetcode-556:下一个更大元素 III
1039 Course List for Student