当前位置:网站首页>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;
}
};
边栏推荐
- 關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
- LVGL 8.2 Sorting a List using up and down buttons
- LVGL 8.2 LED
- Test evaluation of software testing
- Respect others' behavior
- Graduation season - personal summary
- No servers available for service: xxxx
- 一种架构来完成所有任务—Transformer架构正在以一己之力统一AI江湖
- 实战解惑 | OpenCV中如何提取不规则ROI区域
- Yyds dry goods inventory # solve the real problem of famous enterprises: continuous maximum sum
猜你喜欢

Codeforce:c. sum of substrings

10. (map data) offline terrain data processing (for cesium)

Combined with case: the usage of the lowest API (processfunction) in Flink framework

Count the running time of PHP program and set the maximum running time of PHP

RK1126平台OSD的实现支持颜色半透明度多通道支持中文

Talk about 10 tips to ensure thread safety

数据中台概念

Wt588f02b-8s (c006_03) single chip voice IC scheme enables smart doorbell design to reduce cost and increase efficiency

The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese

Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
随机推荐
阿里被裁员工,找工作第N天,猎头又传来噩耗...
Codeforce:c. sum of substrings
MySQL triggers
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
如何配和弦
Node mongodb installation
炒股网上开户安全吗?会不会被骗。
LVLG 8.2 circular scrolling animation of a label
SqlServer函数,存储过程的创建和使用
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
Detailed analysis of pytorch's automatic derivation mechanism, pytorch's core magic
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
flink sql-client. SH tutorial
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
尊重他人的行为
Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
LVGL 8.2 Menu
Progress in architecture