【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/minimum-absolute-difference/
题目描述
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows
a, bare fromarra < bb - aequals to the minimum absolute difference of any two elements inarr
Example 1:
Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
Example 2:
Input: arr = [1,3,6,10,15]
Output: [[1,3]]
Example 3:
Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]
Constraints:
2 <= arr.length <= 10^5-10^6 <= arr[i] <= 10^6
题目大意
给出了一个由不同数字构成的数组,哪些数字的差等于所有数字之差的最小值。
解题方法
排序
这个题肯定要先求所有数字差的最小值,暴力算是O(N^2)不可取。我们知道两个数字差最小,肯定是这两个数字比较接近,所以我们可以先排序,然后找到相邻数字的差的最小值。
找出所有数字差的最小值之后,再遍历一次,找出哪些相邻的数字差等于该最小值就行了。
C++代码如下:
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
const int N = arr.size();
sort(arr.begin(), arr.end());
int min_diff = INT_MAX;
for (int i = 0; i < N - 1; ++i) {
min_diff = min(min_diff, arr[i + 1] - arr[i]);
}
vector<vector<int>> res;
for (int i = 0; i < N - 1; ++i) {
if (arr[i + 1] - arr[i] == min_diff) {
res.push_back({arr[i], arr[i + 1]});
}
}
return res;
}
};
日期
2019 年 9 月 22 日 —— 熬夜废掉半条命
【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)的更多相关文章
- 【leetcode】1200. Minimum Absolute Difference
题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 51. leetcode 530. Minimum Absolute Difference in BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- LeetCode 389 Find the Difference 解题报告
题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- [LeetCode&Python] Problem 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- Python使用print打印时,展示内容不换行
原理 Python的print()函数中参数end='' 默认为\n,所以会自动换行; 默认的print()函数: print(end='\n') 方案 Python 2: 在print语句的末尾加上 ...
- PHP安装PDO_MySQL模块
下载pdo_mysql扩展 wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 解压压缩包 tar -zxvf PDO_MYSQL-1.0.2.tgz 执 ...
- 各个浏览器的webdriver
Chrome 点击下载chrome的webdriver: http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的ch ...
- day10 负载均衡
day10 负载均衡 负载均衡反向代理 正向代理:即是客户端代理, 代理客户端, 服务端不知道实际发起请求的客户端. # (内部上网) 客户端 <-> 代理 -> 服务端 反向代理即 ...
- k8s-hpa自动横向扩容
目录 hpa自动扩容 官方文档 HPA是什么 Horizontal Pod Autoscaler 演练 参数 案例:监控cpu,内存,每秒数据包自动扩容 度量指标 pod清单案例-pod定义cup内存 ...
- Oracle—数据库名、数据库实例名、数据库域名、数据库服务名的区别
Oracle-数据库名.数据库实例名.数据库域名.数据库服务名的区别 一.数据库名 1.什么是数据库名 数据库名就是一个数据库的标识,就像人的身份证号一样.他用参数DB_NAME表示,如果 ...
- 为什么要重写hashcode和equals方法
我在面试 Java初级开发的时候,经常会问:你有没有重写过hashcode方法?不少候选人直接说没写过.我就想,或许真的没写过,于是就再通过一个问题确认:你在用HashMap的时候,键(Key)部分, ...
- Servlet(4):一个简单的注册页面
一. 注册要求 1. 一个注册页面 username (文本框) password:密码 (密码框) passwordYes :再次输入密码(密码框) hobby (多选框) sex (单选框) in ...
- 监控网站是否异常的shell脚本
本节内容:shell脚本监控网站是否异常,如有异常就自动发邮件通知管理员. 脚本检测流程,如下:1,检查网站返回的http_code是否等于200,如不是200视为异常.2,检查网站的访问时间,超过M ...
- Pycharm, 添加requirements.txt
引用:https://www.jetbrains.com/help/pycharm/managing-dependencies.html 1) 2) 3)