当前位置:网站首页>LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
2022-07-04 13:24:00 【HERODING23】

解题思路:
首先排序,由于最小的绝对值差一定是相邻数字之间,所以两两遍历进行比较,设定一个diff差值作为最小差值,遇到更小的就更新,并清空ans,代码如下:
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;
}
};
边栏推荐
- Test evaluation of software testing
- Programmers exposed that they took private jobs: they took more than 30 orders in 10 months, with a net income of 400000
- 电商系统中红包活动设计
- Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
- LVLG 8.2 circular scrolling animation of a label
- [cloud native] how can I compete with this database?
- Ali was laid off employees, looking for a job n day, headhunters came bad news
- C language small commodity management system
- 關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
- 【云原生】我怎么会和这个数据库杠上了?
猜你喜欢

LVGL 8.2 LED

WT588F02B-8S(C006_03)单芯片语音ic方案为智能门铃设计降本增效赋能

LVGL 8.2 Line
![[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL](/img/58/a8dbed993921f372d04f7a1ee19679.png)
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL

Ultrasonic distance meter based on 51 single chip microcomputer

Test evaluation of software testing

Nowcoder reverse linked list

Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?

Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow

Data Lake (13): spark and iceberg integrate DDL operations
随机推荐
LVGL 8.2 Sorting a List using up and down buttons
LVGL 8.2 Line wrap, recoloring and scrolling
Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
LVLG 8.2 circular scrolling animation of a label
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
Opencv3.2 and opencv2.4 installation
曝光一下阿里的工资待遇和职位级别
5G电视难成竞争优势,视频资源成中国广电最后武器
Graduation season - personal summary
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Leetcode t47: full arrangement II
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL
Ali was laid off employees, looking for a job n day, headhunters came bad news
C language programming
Chapter 16 string localization and message Dictionary (2)
LVGL 8.2 Menu
Count the running time of PHP program and set the maximum running time of PHP
AI与生命科学