Skip to content

Commit b47b1fc

Browse files
authored
Create check-if-the-number-is-fascinating.cpp
1 parent 97cc5fe commit b47b1fc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(logn)
2+
// Space: O(1)
3+
4+
// string, bitmask
5+
class Solution {
6+
public:
7+
bool isFascinating(int n) {
8+
int lookup = 0;
9+
const auto& check = [&](int x) {
10+
for (; x ; x /= 10) {
11+
const int d = x % 10;
12+
if (d == 0 || lookup & (1 << d)) {
13+
return false;
14+
}
15+
lookup |= 1 << d;
16+
}
17+
return true;
18+
};
19+
20+
return check(n) && check(2 * n) && check(3 * n);
21+
}
22+
};

0 commit comments

Comments
 (0)