leetcode 1742. 盒子中小球的最大数量_ray.c.l的博客-爱代码爱编程
思路:哈希每个盒子编号对应的小球,然后遍历一遍
代码:
class Solution {
public:
int hash[50];
int countBalls(int lowLimit, int highLimit) {
for (int i = lowLimit; i <= highLimit; i ++) {
int x = cal(i);
hash[x] ++;
}
int res = -1;
for (int i = 0; i < 50; i ++) res = max(res, hash[i]);
return res;
}
int cal(int x) {
int res = 0;
while(x) {
res += x % 10;
x /= 10;
}
return res;
}
};