Leetcode-5197 Minimum Absolute Difference(最小绝对差)

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 #define _rep(i,a,b) for(int i = (a);i > b;i --)
 3 #define INF 0x3f3f3f3f
 4 #define MOD 1000000007
 5 #define pb push_back
 6 #define maxn 10003
 7
 8 class Solution
 9 {
10     public:
11         vector<vector<int>> minimumAbsDifference(vector<int>& arr)
12         {
13             vector<vector<int>> ans;
14             sort(arr.begin(),arr.end());
15             int diff = INF;
16             _for(i,0,arr.size()-1)
17                 diff = min(diff,arr[i+1]-arr[i]);
18
19             vector<int> tmp;
20             _for(i,0,arr.size()-1)
21             {
22                 tmp.clear();
23                 if(arr[i+1]-arr[i]==diff)
24                 {
25                     tmp.pb(arr[i]);
26                     tmp.pb(arr[i+1]);
27                     ans.pb(tmp);
28                 }
29             }
30             return ans;
31         }
32 };

原文地址:https://www.cnblogs.com/Asurudo/p/11566936.html

时间: 2024-08-30 16:44:55

Leetcode-5197 Minimum Absolute Difference(最小绝对差)的相关文章

Leetcode 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

[LeetCode] 530. Minimum Absolute Difference in BST Java

题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and

LeetCode 530. Minimum Absolute Difference in BST(在二叉查找树中查找两个节点之差的最小绝对值)

题意:在二叉查找树中查找两个节点之差的最小绝对值 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int ans = 0x3f3f3f3f; TreeNo

LeetCode Minimum Absolute Difference in BST

原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Ex

【leetcode】1200. 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 -

530. Minimum Absolute Difference in BST(LeetCode)

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

[leetcode]Binary Search Tree-530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

[LeetCode&amp;Python] Problem 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

Binary Search Tree-530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o