-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sum-of-number-and-its-reverse.cpp
63 lines (59 loc) · 1.66 KB
/
sum-of-number-and-its-reverse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Time: O(2^(log10(n)/2)) = O(n^(1/(2*log2(10))))
// Space: O(log10(n)/2)
// backtracking
class Solution {
public:
bool sumOfNumberAndReverse(int num) {
const function<int(int, int)> backtracking = [&](int num, int chosen) {
if (num == 0) {
return true;
}
if (chosen == 1) {
return false;
}
if (num <= 18) {
return (num % 2 == 0) || (num == 11 && chosen == 0);
}
if (chosen == 2) {
return false;
}
for (const auto& x : {num % 10, 10 + num % 10}) {
if (!(1 <= x && x <= 18)) {
continue;
}
int base = 11;
if (chosen) {
base = chosen;
} else {
for (; x * ((base - 1) * 10 + 1) <= num; base = (base - 1) * 10 + 1);
}
if (num - x * base >= 0 && backtracking((num - x * base) / 10, base / 100 + 1)) {
return true;
}
}
return false;
};
return backtracking(num, 0);
}
};
// Time: O(nlogn)
// Space: O(1)
// brute force
class Solution2 {
public:
bool sumOfNumberAndReverse(int num) {
const auto& reverse = [](int n) {
int result = 0;
for (; n; n /= 10) {
result = result * 10 + n % 10;
}
return result;
};
for (int x = num / 2; x <= num; ++x) {
if (x + reverse(x) == num) {
return true;
}
}
return false;
}
};