LeetCode #1200. Minimum Absolute Difference
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode #1200. Minimum Absolute Difference相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%bb%a3%e7%a0%81' title='浠g爜'>浠g爜
lis app href ref 閬嶅巻瀛楀吀 return棰樼洰
1200. Minimum Absolute Difference
瑙i鏂规硶
鍏堝arr鎺掑簭锛屽啀閬嶅巻arr锛屽皢鍏朵腑鐨勬暟瀛樺叆瀛楀吀浣滀负閿紝骞朵笖璁$畻鏈€灏忓樊鍊糾indif銆傛渶鍚庨亶鍘嗗瓧鍏镐腑鐨勯敭key锛屽鏋渒ey+mindif涔熷湪瀛楀吀涓紝鍒欏姞鍏ュ埌杩斿洖鍊紃at銆?br>
鏃堕棿澶嶆潅搴︼細O(nlogn)
绌洪棿澶嶆潅搴︼細O(n)
浠g爜
class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
dic = {}
mindif = arr[1] - arr[0]
for i in range(len(arr)-1):
dic[arr[i]] = arr[i]
mindif = min(mindif, arr[i+1] - arr[i])
else:
dic[arr[i+1]] = arr[i+1]
rat = []
for key in dic.keys():
if key + mindif in dic:
rat.append([key, key+mindif])
return rat
以上是关于LeetCode #1200. Minimum Absolute Difference的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode #1200. Minimum Absolute Difference