当前位置:网站首页>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;
}
};
边栏推荐
- 1039 Course List for Student
- Redis publish subscribe command line implementation
- [jailhouse article] look mum, no VM exits
- leetcode-556:下一个更大元素 III
- Scope of inline symbol
- CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- RGB LED infinite mirror controlled by Arduino
- 【Rust 笔记】15-字符串与文本(下)
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
猜你喜欢

CF1637E Best Pair

Wazuh开源主机安全解决方案的简介与使用体验
![[article de jailhouse] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[article de jailhouse] jailhouse hypervisor

做 SQL 性能优化真是让人干瞪眼

SQLMAP使用教程(一)

Data visualization chart summary (I)

Dichotomy, discretization, etc

QQ电脑版取消转义符输入表情

How to adjust bugs in general projects ----- take you through the whole process by hand

Implement an iterative stack
随机推荐
In this indifferent world, light crying
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
leetcode-1200:最小绝对差
Daily question 2013 Detect square
Appium自动化测试基础 — Appium测试环境搭建总结
SQLMAP使用教程(二)实战技巧一
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
【Rust 笔记】14-集合(上)
Implement a fixed capacity stack
Dynamic planning solution ideas and summary (30000 words)
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
【Rust 笔记】17-并发(上)
从Dijkstra的图灵奖演讲论科技创业者特点
RGB LED infinite mirror controlled by Arduino
【Rust 笔记】16-输入与输出(上)
The difference between CPU core and logical processor
SPI 详解
2017 USP Try-outs C. Coprimes
leetcode-6108:解密消息