当前位置:网站首页>Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
2022-07-04 14:45:00 【Heroding23】
Comment résoudre le problème:
Trier d'abord,Comme la différence absolue minimale doit être entre les chiffres adjacents,Donc les deux traversées sont comparées,Réglez undiffDifférence comme différence minimale,Mettre à jour si vous rencontrez plus petit,Et viderans,Les codes sont les suivants::
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;
}
};
边栏推荐
- Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
- Leetcode t49: grouping of alphabetic words
- 曝光一下阿里的工资待遇和职位级别
- Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
- 程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
- SqlServer函数,存储过程的创建和使用
- ES6 modularization
- A collection of classic papers on convolutional neural networks (deep learning classification)
- LVGL 8.2 LED
- Redis daily notes
猜你喜欢
如何配和弦
各大主流编程语言性能PK,结果出乎意料
Practical puzzle solving | how to extract irregular ROI regions in opencv
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
Leetcode 61: rotating linked list
Compile oglpg-9th-edition source code with clion
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
Real time data warehouse
[information retrieval] link analysis
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
随机推荐
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
如何搭建一支搞垮公司的技术团队?
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
92. (cesium chapter) cesium building layering
LVGL 8.2 List
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
C language set operation
LVGL 8.2 text shadow
Summary of common problems in development
Red envelope activity design in e-commerce system
C language course design questions
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
Chapter 17 process memory
LVGL 8.2 LED
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
SqlServer函数,存储过程的创建和使用
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
LVGL 8.2 LED
C language programming