leetcode1399
Posted AsenYang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode1399相关的知识,希望对你有一定的参考价值。
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,字典按值排序。
以上是关于leetcode1399的主要内容,如果未能解决你的问题,请参考以下文章
sql server新增自增列有重复值(2005 9.0.1399)
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段