【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, b
are fromarr
a < b
b - a
equals 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 ...
随机推荐
- MariaDB——显示所有数据库列表
显示所有数据库列表:其中,information_schema.performance_schema.test.mysql,这4个库表是数据库系统自带的表,一般不放数据. 进入某个库 切换库,并显示库 ...
- 在VS2008环境下编写C语言DLL,并在C++和C#项目下调用 (转载)
1.编写DLL a)文件--打开--新建项目--Win32,右侧Win32项目,填写好项目名称,点击"下一步", 应用程序类型选择:"DLL(D)",附加选项: ...
- SpringBoot 整合 MyBatis,实现 CRUD 示例
目录 前言 创建项目/模块 SpringBoot Console Application CommandLineRunner SpringBoot 集成 MyBatis 创建数据库/表 配置数据源/连 ...
- 【Redis】Sentinel 哨兵模式
Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...
- 学习java的第十三天
一.今日收获(前两天家里有事,博客都忘了发了,唉) 1.通过看哔哩哔哩看黑马程序员的教学视频,学习了java中的数据类型自动转换.强制转换及注意事项三节 2.简单看了看完全学习手册 二.今日问题 1. ...
- myatoi
atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中.int atoi(const char *nptr) 函数会扫描参数 nptr字符串 ...
- 分配器——allocators
任何容器的构建都离不开分配器,分配器顾名思义就是分割配置内存资源的组件,分配器的效率直接影响力容器的效率. operator new()和malloc() C/C++底层都是通过malloc()调用系 ...
- mysql与clickhouse的字段类型对应表
- oracle keep
语法: min | max(column1) keep (dense_rank first | last order by column2) over (partion by column3); -- ...
- lucene的索引查询
package com.hope.lucene;import org.apache.lucene.document.Document;import org.apache.lucene.document ...