401.Binary Watch
401.Binary Watch
难度:Easy
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。
每个 LED 代表一个 0 或 1,最低位在右侧。
例如,上面的二进制手表读取 “3:25”。
给定一个非负整数 n 代表当前 LED 亮着的数量,返回所有可能的时间。
1
案例:
2
3
输入: n = 1
4
返回: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
5
6
7
注意事项:
8
9
输出的顺序没有要求。
10
小时不会以零开头,比如 “01:00” 是不允许的,应为 “1:00”。
11
分钟必须由两位数组成,可能会以零开头,比如 “10:2” 是无效的,应为 “10:02”。
Copied!
时钟一共四个选择,最多亮三盏灯;分钟一共六个选择,最多亮五盏灯。全部枚举出来即可。
1
class Solution {
2
vector<vector<string>>H={{"0"}, {"1","2","4","8"}, {"3","5","6","9","10"}, {"7","11"}};
3
vector<vector<string>>M={{"00"},{"01","02","04","08","16","32"},{"03","05","06","09","10","12", "17","18","20","24","33","34","36","40","48"},{"07","11","13","14","19","21","22","25","26","28","35","37","38","41","42","44","49","50","52","56"},{"15","23","27","29","30","39","43","45","46","51","53","54","57","58"},{"31","47","55","59"}};
4
5
public:
6
vector<string> readBinaryWatch(int num) {
7
vector<string>res;
8
if(num>8) return res;
9
10
for(int i=max(0,num-5);i<=min(num,3);i++)
11
for(auto h:H[i])
12
for(auto m:M[num-i] )
13
res.push_back(h+":"+m);
14
return res;
15
}
16
};
Copied!
执行用时 :8 ms, 在所有 C++ 提交中击败了82.86%的用户 内存消耗 :8.8MB, 在所有 C++ 提交中击败了44.21%的用户
Copy link