码迷,mamicode.com
首页 > 其他好文 > 详细

1399. Count Largest Group

时间:2020-06-29 20:18:12      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:class   nbsp   规则   相加   dig   span   color   分组   turn   

Given an integer n. Each number from 1 to n is grouped according to the sum of its digits. 

Return how many groups have the largest size.

对1-n的数分组,分组规则是各位相加的和,求个数最多的组的个数。

class Solution(object):
    def count_digits_sum(self, num):
        ans = 0
        while num > 0:
            ans += num %10
            num /= 10
        return ans
    def countLargestGroup(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = {}
        for i in range(1, n + 1, 1):
            c = self.count_digits_sum(i)
            if c in count:
                count[c] += 1
            else:
                count[c] = 1
        max_group_num = 0
        ans = 0
        for key,value in count.items():
            max_group_num = max(max_group_num, value)
        for key,value in count.items():
            if value == max_group_num:
                ans += 1
        return ans

 

1399. Count Largest Group

标签:class   nbsp   规则   相加   dig   span   color   分组   turn   

原文地址:https://www.cnblogs.com/whatyouthink/p/13209908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!