【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 ...
随机推荐
- Java培训班学员如何找工作?如何过试用期?
在本文里,首先将结合我了解的多家培训班辅导学员就业的情况,来讲讲培训班学员如何高效找工作.由于本人在周末会兼职在培训班讲课,也帮助过不少学员成功入职,所以下文还会给出"培训班学员如何快速适应 ...
- C语言按行读入文件
getline() 函数无论一行多长,动态分配内存读入行 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <s ...
- mysql order by 多个字段排序实现组内排序
总结:大组在前,小组在后,计量值再最后,即可实现组内排序:下边是参考别人的具体实例: 工作中需用到order by 后两个字段排序,但结果却产生了一个Bug,以此备录. [1]复现问题场景 为了说明问 ...
- 巩固javawbe第二天
巩固内容: <!DOCTYPE> 声明 <!DOCTYPE>声明有助于浏览器中正确显示网页. 网络上有很多不同的文件,如果能够正确声明HTML的版本,浏览器就能正确显示网页内容 ...
- 报错:Unsupported field: HourOfDay
报错:Unsupported field: HourOfDay 这个错误就比较搞笑也比较低级了. 代码如下 LocalDate now = LocalDate.now(); String year = ...
- 【vector的输出问题】 洛谷 P1996 约瑟夫问题
题目:P1996 约瑟夫问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 可恶啊,本来是一道不算难的题,硬是因为cin,cout同步流卡了我一天qwq 关闭cin,cout同步流 ...
- Druid数据库监控
一.简介 Druid是阿里开源的一个JDBC应用组件, 其包括三部分: DruidDriver: 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource ...
- js中获取url参数
function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location ...
- 【Java多线程】ExecutorService和ThreadPoolExecutor
ExecutorService Java.util.concurrent.ExecutorService接口代表一种异步执行机制,它能够在后台执行任务.因此ExecutorService与thread ...
- 使用beanFactory工厂实例化容器的方式实现单例模式
//配置文件bean.properties(注意书写顺序) accountService=com.itheima.service.impl.AccountServiceImplaccountDao=c ...