虽然是暴力方法,然后用到很多的C++ 新的特性:
将所有的数字各位数字求和,然后放进对应的桶内
class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
auto it = [&](int n) -> int {
int res = 0;
while(n){
res+=n%10;
n = n/10;
}
return res;
};
map<int,int> map2;
for(int i=lowLimit;i<=highLimit;i++) {
map2[it(i)]++;
}
int res = 0;
for(auto &[_,c]:map2) {
if(c>res) {
res = c;
}
}
return res;
}
};