A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
For example, the above binary watch reads "3:25".
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
Note:
- The order of output does not matter.
- The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
- The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
该问题相当于已知一个数组a[]={1,2,4,8},b[]={1,2,4,8,16,32}
输入一个数字num,分别从数组a和数组b中取数字,取出的数字总个数为num个,a中取出的数字和范围0-11,b中取出的数字和范围0-59
因此问题转换为,从一个大小为size的数组中取count个数字的组合问题,通过getCombination()函数获得,这是解决问题的关键
【代码】:
class Solution {
public:
vector<string> readBinaryWatch(int num) {
if (num >= 10) {
return res;
}
int hour[] = {1,2,4,8};
int min[] = {1,2,4,8,16,32};
std::vector<int> hours(hour,hour+4);
std::vector<int> mins(min,min+6);
std::vector<std::string> str_hour;
std::vector<std::string> str_min;
for (int i = 0; i <= num; ++i) {
comb_res.clear();
str_hour.clear();
set_max_value(11);
getCombination(0,0,i, hours); //i个数字组成小时数
for(std::set<int>::iterator iter = comb_res.begin(); iter != comb_res.end(); ++iter) {
str_hour.push_back(timeToStr(*iter, true, false));
}
comb_res.clear();
str_min.clear();
set_max_value(59);
getCombination(0,0,num-i, mins); //num-i个数字组成分钟数
for(std::set<int>::iterator iter = comb_res.begin(); iter != comb_res.end(); ++iter) {
str_min.push_back(timeToStr(*iter, false, true));
}
for (int i = 0; i < str_hour.size(); ++i) {
for (int j = 0; j < str_min.size(); ++j) {
res.push_back(str_hour[i] + ":" + str_min[j]);//拼接时间
//std::cout << str_hour[i] + ":" + str_min[j] << "\t";
}
}
//std::cout << std::endl;
}
return res;
}
std::string timeToStr(int time, bool hour, bool min) { //int转小时或分钟数
std::string str;
std::stringstream ss;
ss << time;
if (hour) {
ss >> str;
}
if (min) {
ss >> str;
if (time < 10) {
str = "0" + str;
}
}
return str;
}
//begin:nums起始位置,count:当前取的数字个数,max_count:需要获取的数字个数,nums:存储数字的数组
void getCombination(int begin, int count, int max_count, std::vector<int> nums) {
if (max_count < 0)
return;
if (count == max_count) {
int tmp = sum();
if (!comb_res.count(tmp) && tmp <= max_value)
comb_res.insert(tmp);
return ;
}
for (int i = begin; i < nums.size(); ++i) {
comb_vec.push_back(nums[i]);//包含数字nums[i]
count++;
getCombination(i+1, count, max_count, nums);
comb_vec.pop_back(); //不包含数字nums[i]
count--;
getCombination(i+1, count, max_count, nums);
}
}
int sum() {
int sum = 0;
for (int i = 0; i < comb_vec .size(); ++i) {
sum += comb_vec[i];
}
return sum;
}
void set_max_value(int value) {
max_value = value;
}
private:
int count;
int max_value;
std::vector<int> comb_vec;
std::set<int> comb_res;
std::vector<std::string> res;
};