1399. Count Largest Group

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
原文地址:https://www.cnblogs.com/whatyouthink/p/13209908.html