C++ 13 ms simple solution. Precalculate results for every minute of the day and save them to cache


  • 1
    I

    It can be optimized. The goal was to get it accepted for the contest.

    class Solution {
    public:
        Solution()
        {
            res.resize(11);
            for(int i = 0; i <= 11; ++i)
            for(int j = 0; j <= 59; ++j)
            {
                stringstream ss;
                ss << setw(2) << setfill('0') << j;
                string min = ss.str();
                stringstream ss1;
                ss1 << i << ":" << min;
                int n = numbits(i) + numbits(j);
                res[n].push_back(ss1.str());
            }
            
        }
        int numbits(int n)
        {
            int ans = 0;
            for(int i = 0; i < 7; ++i)
            {
                ans += (n & 1);
                n = n >> 1;
            }
            return ans;
        }
    
        vector<string> readBinaryWatch(int num) {
            
            return res[num];
        }
        vector<vector<string>> res;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.