当前位置:网站首页>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;
}
};
边栏推荐
- Is it safe to open an account online for stock speculation? Will you be cheated.
- Data Lake (13): spark and iceberg integrate DDL operations
- scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
- C language achievement management system for middle school students
- 【云原生】我怎么会和这个数据库杠上了?
- Nowcoder rearrange linked list
- LVGL 8.2 Draw label with gradient color
- LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
- 程序员自曝接私活:10个月时间接了30多个单子,纯收入40万
- LVGL 8.2 text shadow
猜你喜欢
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
数据湖(十三):Spark与Iceberg整合DDL操作
Digi restarts XBee Pro S2C production. Some differences need to be noted
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
Digi XBee 3 rf: 4 protocols, 3 packages, 10 major functions
电商系统中红包活动设计
Query optimizer for SQL optimization
随机推荐
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
Programmer turns direction
关于FPGA底层资源的细节问题
Node mongodb installation
Compile oglpg-9th-edition source code with clion
Real time data warehouse
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
Combined with case: the usage of the lowest API (processfunction) in Flink framework
各大主流编程语言性能PK,结果出乎意料
软件测试之测试评估
Deep learning 7 transformer series instance segmentation mask2former
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
C language achievement management system for middle school students
Sqlserver functions, creation and use of stored procedures
SqlServer函数,存储过程的创建和使用
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
Explain of SQL optimization
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!