leetcode 401. Binary Watch

class Solution {
public:
    vector<string> readBinaryWatch(int num) {
        vector<string> res[11];
        vector<string> ans[11];
        vector<string> result;
        if(num > 10 || num < 0) return result;
        for(int i = 0;i <= 11; i++){
            int j = i, c = 0;
            while(j){
                if(j&1) c++;
                j/=2;
            }
            string x="";
            j = i;
            do{
                x = char('0'+j%10)+x;
                j/=10;
            }while(j>0);
            res[c].push_back(x);
        }
        for(int i = 0;i <= 59;i++){
            int j = i, c = 0;
            while(j){
                if(j&1)c++;
                j/=2;
            }

            string x="";
            j = i;
            for(int k = 0;k < 2; k++){
                x = char('0'+j%10)+x;
                j/=10;
            }
            ans[c].push_back(x);
        }
        for(int i = 0;i <= num; i++){
            string x="";
            for(int j = 0;j < res[i].size(); j++){
                for(int k = 0;k < ans[num-i].size(); k++){
                    x = res[i][j]+":"+ans[num-i][k];
                    result.push_back(x);
                }
            }
        }
        return result;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章