leetcode1366

 1 class Solution:
 2     def rankTeams(self, votes: 'List[str]') -> str:
 3         dic = {}
 4         n = len(votes)
 5         m = len(votes[0])
 6         mat = [[0 for _ in range(26)] for _ in range(26)]
 7         for i in range(n):
 8             cur = votes[i]
 9             for j in range(m):
10                 vote = cur[j]
11                 code = ord(vote) - 65#row number
12                 mat[code][j] += 1
13         #print(mat)
14         for i in range(26):
15             key = ''
16             for j in range(26):
17                 cur = str(mat[i][j]).zfill(4)
18                 #print(cur)
19                 key += cur
20             #print(key)
21             if key != '0' * 26 * 4:
22                 if key not in dic:
23                     dic[key] = [chr(i + 65)]
24                 else:
25                     dic[key].append(chr(i + 65))
26         li = sorted(dic.items(),key=lambda x:x[0],reverse = True)
27         result = []
28         for l in li:
29             temp = sorted(l[1])
30             result += temp
31         return ''.join(result)

算法思路:使用26 * 26的矩阵,存储每一个字符的每一个排名的次数。

然后遍历这个矩阵,每一行生成一个4*26的key,根据key倒序排列。同样的key的字符,再按照字符顺序排列。

上一篇:laravel5.8(三)数据库相关操作及配置


下一篇:实验3 函数