力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/description/
虽然是暴力方法,然后用到很多的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;
}
};