web123456

Binary time - LintCode

#ifndef C706_H #define C706_H #include<iostream> #include<string> #include<vector> using namespace std; class Solution { public: /* * @param : the number of "1"s on a given timetable * @return: all possible time */ vector<string> binaryTime(int num) { // Write your code here vector<string> res; //num is meaningful between 0 and 8 if (num == 0) return{ "0:00" }; if (num >= 9) return res; vector<vector<int>> dic(6, vector<int>());// Store numbers containing 0-5 1's for (int i = 0; i < 60; ++i) dic[countOne(i)].push_back(i); //Processing from the hourly portion for (int i = 0; i <= 3; ++i) { //Some conditions to be met if (num - i <= 5 && num - i >= 0) { //Processing minutes section for (auto c : dic[i]) { if (c < 12) { for (auto t : dic[num - i]) { string str; if (t >= 10) str = to_string(c) + ":" + to_string(t); else str = to_string(c) + ":0" + to_string(t); res.push_back(str); } } } } } return res; } //Calculate the number of digits containing a 1. int countOne(int num) { int cnt = 0; while (num != 0) { if (num & 1 != 0) cnt++; num = num >> 1; } return cnt; } }; #endif