mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
add 401
This commit is contained in:
parent
d36220bd1d
commit
a7aab80fa0
@ -287,6 +287,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。
|
||||
| 397 |[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/397.%20Integer%20Replacement.md)|Medium| |
|
||||
| 398 |[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/398.%20Random%20Pick%20Index.md)|Medium| |
|
||||
| 400 |[Nth Digit](https://leetcode.com/problems/nth-digit)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/400.%20Nth%20Digit.md)|Medium| |
|
||||
| 401 |[Binary Watch](https://leetcode.cn/problems/binary-watch/)|[C++](solutions/401.%20Binary%20Watch.md)|Easy| |
|
||||
| 402 |[Remove K Digits](https://leetcode.cn/problems/remove-k-digits/)|[C++](solutions/402.%20Remove%20K%20Digits.md)|Medium| |
|
||||
| 404 |[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/404.%20Sum%20of%20Left%20Leaves.md)|Easy| |
|
||||
| 405 |[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/405.%20Convert%20a%20Number%20to%20Hexadecimal.md)|Easy| |
|
||||
|
33
solutions/401. Binary Watch.md
Normal file
33
solutions/401. Binary Watch.md
Normal file
@ -0,0 +1,33 @@
|
||||
# [401. Binary Watch](https://leetcode.cn/problems/binary-watch/)
|
||||
|
||||
# 思路
|
||||
由于分钟和小时都是有限且很小的,所以我们可以遍历一遍所有分钟,检查其二进制表示中1的个数即可。是个简单题,不要考虑太复杂。
|
||||
|
||||
# C++
|
||||
```C++
|
||||
class Solution {
|
||||
private:
|
||||
int bitCount(int num){
|
||||
int res = 0;
|
||||
while(num > 0){
|
||||
res += (num % 2);
|
||||
num /= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public:
|
||||
vector<string> readBinaryWatch(int turnedOn) {
|
||||
vector<string>res;
|
||||
for(int i = 0; i < 12; i++){
|
||||
for(int j = 0; j < 60; j++){
|
||||
if(bitCount(i) + bitCount(j) == turnedOn){
|
||||
res.push_back(
|
||||
to_string(i) + (j < 10 ? ":0" : ":") + to_string(j)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in New Issue
Block a user