leetcode1200

Posted asenyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode1200相关的知识,希望对你有一定的参考价值。

 1 import sys
 2 class Solution:
 3     def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
 4         n = len(arr)
 5         arr = sorted(arr)
 6         if n == 2:
 7             return [arr]
 8         dic = 
 9         minval = sys.maxsize
10         for i in range(n-1):
11             j = i + 1
12             diff = arr[j] - arr[i]
13             if diff <= minval:
14                 minval = diff
15                 if diff not in dic:
16                     dic[diff] = [[arr[i],arr[j]]]
17                 else:
18                     dic[diff].append([arr[i],arr[j]])
19                 
20         return dic[minval]

先将数据排序,然后计算相邻数组的差值,使用字典保存最小差值所包含的元素对。

以上是关于leetcode1200的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路

LeetCode --- 1200. Minimum Absolute Difference 解题报告

leetcode1200. Minimum Absolute Difference

LeetCode --- 1200. Minimum Absolute Difference 解题报告

LeetCode #1200. Minimum Absolute Difference

leetcode1200. 最小绝对差