leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE | |
commit | 6dd55f3f9a575cbba21cea088673999e733ed146 |
parent | 137f3d4b6631ffc3cb3abc47ae2cf2e6da8791e1 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Wed, 16 Oct 2024 15:06:29 +0200 |
1 Random Problem
Diffstat:A | Problems/0401.cpp | | | +++++++++++++++++++++++++++ |
M | README.md | | | + |
2 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/Problems/0401.cpp b/Problems/0401.cpp
@@ -0,0 +1,27 @@
class Solution {
inline static string convert(int n) {
if (n < 10) return '0' + to_string(n);
return to_string(n);
}
public:
vector<string> readBinaryWatch(int turnedOn) const {
if (turnedOn == 0) return {"0:00"};
vector<string> res;
unsigned i = (1 << turnedOn) - 1, t;
while (i < (1 << 10)) {
const int hours = i >> 6, minutes = i & 0x3F;
if (hours < 12 && minutes < 60) {
res.push_back(to_string(hours) + ':' + convert(minutes));
}
const unsigned t = i | (i - 1);
i = (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(i) + 1));
}
return res;
}
};
diff --git a/README.md b/README.md
@@ -313,6 +313,7 @@ for solving problems.
| 0394 | Medium | [Decode String](Problems/0394.cpp) |
| 0398 | Medium | [Random Pick Index](Problems/0398.cpp) |
| 0399 | Medium | [Evaluate Division](Problems/0399.cpp) |
| 0401 | Easy | [Binary Watch](Problems/0401.cpp) |
| 0402 | Medium | [Remove K Digits](Problems/0402.cpp) |
| 0403 | Hard | [Frog Jump](Problems/0403.cpp) |
| 0404 | Easy | [Sum of Left Leaves](Problems/0404.cpp) |