classSolution: defminimumAbsDifference(self, arr: List[int]) -> List[List[int]]: min_ = float("inf") res = [] arr.sort() # 排序之后从前往后进行遍历 排序之后找最小差值 只需要在相邻的两个数之间进行寻找判断即可 for i inrange(1,len(arr)): if min_ > abs(arr[i]-arr[i-1]): min_ = abs(arr[i]-arr[i-1]) res = [[arr[i-1],arr[i]]] elif min_== abs(arr[i]-arr[i-1]): res.append([arr[i-1],arr[i]]) return res
2843、统计对称整数的数目
数字转化为字符串进行判断再求和 复杂度较高 可以进行优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defcountSymmetricIntegers(self, low: int, high: int) -> int: defjudge(num): num = str(num) a = list(num[:len(num)//2]) b = list(num[len(num)//2:]) a = [int(x) for x in a] b = [int(x) for x in b] returnsum(a)==sum(b) res = 0 for i inrange(low,high+1): iflen(str(i))%2!=0: continue if judge(i): res+=1 return res