C++ solution using unordered_map<int, vector<string>>


  • 0
    M
    class Solution {
    public:
        vector<string> readBinaryWatch(int num) {
            unordered_map<int, vector<string>> myMap;
            int temp;
            for(int i = 0; i <= 11; ++i){
                for(int j = 0; j <= 9; ++j){
                    temp = numOfOnes(i) + numOfOnes(j);
                    myMap[temp].push_back(to_string(i) + ":0" + to_string(j));
                }
                for(int j = 10; j <= 59; ++j){
                    temp = numOfOnes(i) + numOfOnes(j);
                    myMap[temp].push_back(to_string(i) + ":" + to_string(j));
                }            
            }
            return myMap[num];
        }
        
        int numOfOnes(size_t n){
            int counter = 0;
            while(n){
                counter += n%2;
                n /= 2;
            }
            return counter;
        }
    };
    

Log in to reply
 

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