1 class Solution:
 2     def calSum(self,x):
 3         s = str(x)
 4         sums = 0
 5         for j in range(len(s)):
 6             sums += int(s[j])
 7         return sums
 8         
 9     def countLargestGroup(self, n: int) -> int:
10         dic = dict()
11         for i in range(1,n+1):
12             k = self.calSum(i)
13             if k not in dic:
14                 dic[k] = 1
15             else:
16                 dic[k] += 1
17         tp = sorted(dic.items(), key=lambda x: x[1], reverse=True)
18         maxlen = tp[0][1]
19         count = 0
20         for i in range(len(tp)):
21             if tp[i][1] == maxlen:
22                 count += 1
23             else:
24                 break
25         return count

算法思路:hash,字典按值排序。

posted on 2020-04-05 04:55  Sempron2800+  阅读(168)  评论(0编辑  收藏  举报