1 class Solution:
 2     def arrayRankTransform(self, arr: List[int]) -> List[int]:
 3         n = len(arr)
 4         if n == 0:
 5             return []
 6         sortlist = sorted(arr)
 7         dic = {}
 8         dic[sortlist[0]] = 1
 9         for i in range(1,n):
10             pre = sortlist[i-1]
11             cur = sortlist[i]
12             if cur == pre:
13                 dic[cur] = dic[pre]
14             else:
15                 dic[cur] = dic[pre] + 1
16         result = []
17         for a in arr:
18             result.append(dic[a])
19         return result

算法思路:将数组排序,使用字典记录每一个数值的相对顺序。

posted on 2020-01-26 05:51  Sempron2800+  阅读(100)  评论(0编辑  收藏  举报