20250213力扣每日一题

知识点
解题思路
实现代码
我的代码
class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
unordered_map<int, int> boxes;
for(int i = lowLimit; i <= highLimit; i ++){
int sum = 0;
int tmp = i;
while(tmp != 0){
sum += (tmp % 10);
tmp /= 10;
}
if(boxes.count(sum)){
boxes[sum] ++;
}else{
boxes.insert({sum, 1});
}
}
vector<pair<int,int>> ans(boxes.begin(), boxes.end());
int MaxCnt = 0, ansIndex = 0;
for(auto p: ans){
MaxCnt = max(p.second, MaxCnt);
}
return MaxCnt;
}
};
题解中的代码
class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
unordered_map<int, int> cnt;
int res = 0;
for(int i = lowLimit; i <= highLimit; i ++){
int box = 0, x = i;
while(x){
box += x % 10;
x /= 10;
}
cnt[box] ++;
res = max(res, cnt[box]);
}
return res;
}
};
Enjoy Reading This Article?
Here are some more articles you might like to read next: