当前位置:网站首页>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;
}
};
边栏推荐
- 多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
- Daily question 2006 Number of pairs whose absolute value of difference is k
- How to adjust bugs in general projects ----- take you through the whole process by hand
- 927. Trisection simulation
- [rust notes] 17 concurrent (Part 2)
- Implement a fixed capacity stack
- Implement an iterative stack
- 从Dijkstra的图灵奖演讲论科技创业者特点
- Typical use cases for knapsacks, queues, and stacks
- LeetCode 0107.二叉树的层序遍历II - 另一种方法
猜你喜欢
Introduction et expérience de wazuh open source host Security Solution
Sqlmap tutorial (II) practical skills I
API related to TCP connection
Groupbykey() and reducebykey() and combinebykey() in spark
数据可视化图表总结(二)
SPI details
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
QQ电脑版取消转义符输入表情
R语言【数据集的导入导出】
Appium automation test foundation - Summary of appium test environment construction
随机推荐
1039 Course List for Student
QQ电脑版取消转义符输入表情
剑指 Offer II 058:日程表
Groupbykey() and reducebykey() and combinebykey() in spark
927. Trisection simulation
PC register
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Liunx starts redis
1041 Be Unique
【Rust 笔记】14-集合(下)
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Daily question 1688 Number of matches in the competition
CF1634 F. Fibonacci Additions
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
Doing SQL performance optimization is really eye-catching
Typical use cases for knapsacks, queues, and stacks
leetcode-6109:知道秘密的人数
leetcode-6108:解密消息
数据可视化图表总结(二)
Appium automation test foundation - Summary of appium test environment construction