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 - a equals to the minimum absolute difference of any two elements in arr

排序,求最小的diff,然后for一遍看看相邻的元素有多少的diff等于最小diff

class Solution(object):
    def minimumAbsDifference(self, arr):
        """
        :type arr: List[int]
        :rtype: List[List[int]]
        """
        import sys
        arr = sorted(arr)
        diff = sys.maxint
        ans = []
        for i in range(1, len(arr), 1):
            diff = min(diff, arr[i] - arr[i - 1])
        for i in range(1, len(arr), 1):
            if arr[i] - arr[i - 1] == diff:
                ans.append([arr[i - 1], arr[i]])
        return ans
            
原文地址:https://www.cnblogs.com/whatyouthink/p/13209012.html