当前位置:网站首页>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;
}
};
边栏推荐
- CPU内核和逻辑处理器的区别
- leetcode-31:下一个排列
- 可变电阻器概述——结构、工作和不同应用
- [jailhouse article] performance measurements for hypervisors on embedded ARM processors
- Daily question 1342 Number of operations to change the number to 0
- MIT-6874-Deep Learning in the Life Sciences Week 7
- 1039 Course List for Student
- 数据可视化图表总结(二)
- Collection: programming related websites and books
- 【Rust 笔记】17-并发(下)
猜你喜欢

leetcode-6111:螺旋矩阵 IV

redis发布订阅命令行实现

开源存储这么香,为何我们还要坚持自研?

Smart construction site "hydropower energy consumption online monitoring system"

CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five

【实战技能】如何做好技术培训?

Time of process

【Jailhouse 文章】Jailhouse Hypervisor

wordpress切换页面,域名变回了IP地址

Is it impossible for lamda to wake up?
随机推荐
PC register
2020ccpc Qinhuangdao J - Kingdom's power
leetcode-22:括号生成
数据可视化图表总结(二)
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
CPU内核和逻辑处理器的区别
JS quickly converts JSON data into URL parameters
Fried chicken nuggets and fifa22
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
[cloud native] record of feign custom configuration of microservices
Multi screen computer screenshots will cut off multiple screens, not only the current screen
Personal developed penetration testing tool Satania v1.2 update
Redis publish subscribe command line implementation
1996. number of weak characters in the game
884. Uncommon words in two sentences
Some common problems in the assessment of network engineers: WLAN, BGP, switch
[article de jailhouse] jailhouse hypervisor
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
Appium automation test foundation - Summary of appium test environment construction
Transform optimization problems into decision-making problems