当前位置:网站首页>Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
2022-07-04 14:46:00 【HERODING23】
Their thinking :
Sort first , Because the smallest absolute value difference must be between adjacent numbers , So compare two iterations , Set up a diff The difference is taken as the minimum difference , Update when encountering smaller ones , And empty ans, The code is as follows :
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
int n = arr.size();
sort(arr.begin(), arr.end());
int diff = arr[1] - arr[0];
vector<vector<int>> ans;
for(int i = 1; i < n; i ++) {
if(arr[i] - arr[i - 1] == diff) {
ans.push_back({
arr[i - 1], arr[i]});
} else if(arr[i] - arr[i - 1] < diff) {
diff = arr[i] - arr[i - 1];
ans.clear();
ans.push_back({
arr[i - 1], arr[i]});
} else continue;
}
return ans;
}
};
边栏推荐
- Data Lake (13): spark and iceberg integrate DDL operations
- Nowcoder rearrange linked list
- ES6 modularization
- Combined with case: the usage of the lowest API (processfunction) in Flink framework
- Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
- Leetcode 61: rotating linked list
- openresty 重定向
- 深度学习7 Transformer系列实例分割Mask2Former
- remount of the / superblock failed: Permission denied
- 关于FPGA底层资源的细节问题
猜你喜欢
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
LVGL 8.2 text shadow
Leetcode T48: rotating images
函数计算异步任务能力介绍 - 任务触发去重
Nowcoder rearrange linked list
Gin integrated Alipay payment
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
LVGL 8.2 Draw label with gradient color
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
随机推荐
A keepalived high availability accident made me learn it again
《opencv学习笔记》-- 线性滤波:方框滤波、均值滤波、高斯滤波
MySQL stored procedure exercise
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
Ali was laid off employees, looking for a job n day, headhunters came bad news
How to match chords
leetcode:6109. Number of people who know the secret [definition of DP]
各大主流编程语言性能PK,结果出乎意料
Compile oglpg-9th-edition source code with clion
Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
Leetcode t49: grouping of alphabetic words
程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
电商系统中红包活动设计
Digi XBee 3 RF: 4个协议,3种封装,10个大功能
First experience of ViewModel
Respect others' behavior
ES6 modularization
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions