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:

  • Google Gemini updates: Flash 1.5, Gemma 2 and Project Astra
  • Displaying External Posts on Your al-folio Blog
  • 强化学习导论
  • 企业项目实训
  • 面试总结