题目地址: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 from arr
  • a < b
  • b - a equals to the minimum absolute difference of any two elements in arr

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:

  1. 2 <= arr.length <= 10^5
  2. -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++)的更多相关文章

  1. 【leetcode】1200. Minimum Absolute Difference

    题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute ...

  2. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  3. 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 ...

  4. 【LeetCode】539. Minimum Time Difference 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...

  5. LeetCode 389 Find the Difference 解题报告

    题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...

  6. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  8. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  9. [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 ...

随机推荐

  1. doxygen文件配置

    主要配置修改 整个程序配置分几个部分 Project related configuration options 项目相关,包括: 项目名 输出目录 输出语言 是否显示继承属性 是否对C.Java.F ...

  2. python-django-自定义查询Q函数和F函数

    数据库: def page_q(request): """Q函数的使用""" #查询username和nickname都是zhangsan ...

  3. 毕业设计之zabbix之nginx状态监控

    监控脚本: [root@webone.quan.bbs ~]$vim /usr/local/zabbix/script/ngx_status.sh #!/bin/bash##************* ...

  4. fping (比ping更强大的ping工具)

    Fping程序类似于ping(ping是通过ICMP(网络控制信息协议InternetControl Message Protocol)协议回复请求以检测主机是否存在).Fping与ping不同的地方 ...

  5. 30-Container With Most Water-Leetcode

    Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). n ...

  6. 学习java的第五天

    一.今日收获 1.java程序设计完全手册第一章节的小总结 2.完成了部分例题验证解答. 二.今日难题 1.java语言与c++语言的不同点. 2.有例题验证不出来 三.明日目标 1.复习java程序 ...

  7. day04:Python学习笔记

    day04:Python学习笔记 1.算数运算符 1.算数运算符 print(10 / 3) #结果带小数 print(10 // 3) #结果取整数,不是四舍五入 print(10 % 3) #结果 ...

  8. 什么是 IP 地址 – 定义和解释

    IP 地址定义 IP 地址是一个唯一地址,用于标识互联网或本地网络上的设备.IP 代表"互联网协议",它是控制通过互联网或本地网络发送的数据格式的一组规则. 本质上,IP 地址是允 ...

  9. vue-cli4脚手架搭建二

    vue-cli4脚手架搭建一 vue create vue3 cd vue3 yarn serve http://localhost:8080/#/ main.js文件 import Vue from ...

  10. 通过SSE(Server-Send Event)实现服务器主动向浏览器端推送消息

    一.SSE介绍 1.EventSource 对象 SSE 的客户端 API 部署在EventSource对象上.下面的代码可以检测浏览器是否支持 SSE. if ('EventSource' in w ...