401. Binary Watch
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”.
Solution in C++:
关键点:
- 位数与数字的对应
思路:
- 我的思路有点复杂,其实总的来说是不知道STL的使用的锅。先说下我的大概思路,首先构造分钟和时钟分别不同位数对应的不同字符串。然后根据时钟最多4位,分钟最多6位的限制,进行整数划分。其中包含了几个操作,首先是初始化了tables用于存储每个数字含有几个bit的1;bits函数用于计算num中含有bit的数目,然后根据整数划分直接读取map中的值就OK了。
- 看到discuss里面有直接关于bit的STL bitset我真的是学到了,这题花了我一个多小时。。。今天注定刷不了几题了。
方法一:手动求num->bits
vector<int> tables;
int bits(int num){
int result = 0;
while(num){
++result;
num &= (num - 1);
}
return result;
}
void init(vector<int> &t){
for(int i = 0; i <= 59; ++i)
tables.push_back(bits(i));
}
vector<string> readBinaryWatch(int num) {
init(tables);
vector<string> result, str;
map<int,vector<string>> hours, minutes;
// 计算时钟字符串
for(int i = 0; i <= 4; ++i){
str.clear();
vector<int> tmp;
for(int j = 0; j <= 11; ++j)
if(tables[j] == i)
tmp.push_back(j);
for(auto t : tmp){
str.push_back(to_string(t) + ":");
}
hours[i] = str;
}
// 计算分钟字符串
for(int j = 0; j <= 6; ++j){
str.clear();
vector<int> tmp;
for(int i = 0; i <= 59; ++i)
if(tables[i] == j)
tmp.push_back(i);
for(auto t : tmp){
if (t < 10)
str.push_back("0"+to_string(t));
else
str.push_back(to_string(t));
}
minutes[j] = str;
}
// 整数划分 时钟部分:0-4 分钟:0-6
for(int i = 0; i <= 4; ++i){
int j = num - i;
if ((j <= 6) && (j >= 0)){
for(auto h : hours[i]){
for(auto m : minutes[j])
result.push_back(h + m);
}
}
}
return result;
}
方法二:bitset
vector<string> readBinaryWatch(int num) {
vector<string> result;
for(int h = 0; h < 12; ++h){
for (int m = 0; m < 60; ++m){
if (bitset<10>(h<<6 | m).count() == num)
result.emplace_back(to_string(h) + (m < 10?":0":":") + to_string(m));
}
}
return result;
}
405. Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complementmethod is used.
Note:
- All letters in hexadecimal (
a-f) must be in lowercase. - The hexadecimal string must not contain extra leading
0s. If the number is zero, it is represented by a single zero character'0'; otherwise, the first character in the hexadecimal string will not be the zero character.The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0’; otherwise, the first character in the hexadecimal string will not be the zero character. - The given number is guaranteed to fit within the range of a 32-bit signed integer.The given number is guaranteed to fit within the range of a 32-bit signed integer.
- You must not use any method provided by the library which converts/formats the number to hex directly.You must not use any method provided by the library which converts/formats the number to hex directly.
Example 1:
Input:
26
Output:
"1a"
Example 2:
Input:
-1
Output:
"ffffffff"
Solution in C++:
关键点:
- 计算机中数的表示
思路:
- 看了2的补码wiki,得到计算负数 n u m num num的补码的数值大小为 2 n + n u m 2^n+num 2n+num,但是int的范围是[- 2 31 2^{31} 231, 2 31 − 1 2^{31}-1 231−1],所以这里表示的时候需要通过long long类型的变量来转存。这里的思路就是小于0时令变量为 2 n + n u m 2^n+num 2n+num否则为 n u m num num即可。其他按进制转换操作来。
- 看到discuss里面很多人都是通过强制类型转换为unsigned int来实现的。
方法一: 2 n + n u m 2^n+num 2n+num
string toHex(int num) {
if (num == 0)
return "0";
string result;
long long tmp = num;
// 负数
if (num < 0){
tmp = pow(2,32) + num;
}
while(tmp){
int n = tmp % 16;
if (n >= 10){
char ch = 'a' + n - 10;
result = ch + result;
} else{
result = to_string(n) + result;
}
tmp /= 16;
}
return result;
}
方法二:强制类型转换为unsigned
string helper(unsigned int num){
string result;
while(num){
int n = num % 16;
if (n >= 10){
char ch = 'a' + n - 10;
result = ch + result;
} else{
result = to_string(n) + result;
}
num /= 16;
}
return result;
}
string toHex(int num) {
return helper(num);
}
小结
哎,今天成就感蛮低的,首先题目数量刷的不多意味着每题的耗时比较长,然后就是基本没完全做出来,不过好在写博客的时候自己写的代码都调试出来了。不然真的是满满的挫败感啊。
知识点
- bit-num映射 及 bitset
- 补码及强制类型转换unsigned
本文深入探讨了二进制手表时间表示的算法实现,以及将整数转换为十六进制字符串的方法,包括处理负数的二进制补码表示。文章提供了C++代码示例,展示了如何利用位操作和STL bitset进行高效计算。

被折叠的 条评论
为什么被折叠?



