diff --git a/README.md b/README.md index 0a06129..4c54c2b 100644 --- a/README.md +++ b/README.md @@ -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| | diff --git a/solutions/401. Binary Watch.md b/solutions/401. Binary Watch.md new file mode 100644 index 0000000..3c83ec7 --- /dev/null +++ b/solutions/401. Binary Watch.md @@ -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 readBinaryWatch(int turnedOn) { + vectorres; + 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; + } +}; +``` \ No newline at end of file