forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-the-closest-palindrome.cpp
28 lines (27 loc) · 1021 Bytes
/
find-the-closest-palindrome.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
// Time: O(l)
// Space: O(l)
class Solution {
public:
string nearestPalindromic(string n) {
const auto l = n.size();
unordered_set<long long> candidates;
candidates.emplace(static_cast<long long>(pow(10, l)) + 1);
candidates.emplace(static_cast<long long>(pow(10, l - 1)) - 1);
auto prefix = stol(n.substr(0, (l + 1) / 2));
for (long long i = -1; i <= 1; ++i) {
auto p = to_string(prefix + i);
auto pp = p + string(p.rbegin() + (l % 2), p.rend());
candidates.emplace(stol(pp));
}
long long num = stol(n), closest_val = numeric_limits<long long>::max();
candidates.erase(num);
for (const auto& val : candidates) {
if (abs(val - num) < abs(closest_val - num)) {
closest_val = val;
} else if (abs(val - num) == abs(closest_val - num)) {
closest_val = min(closest_val, val);
}
}
return to_string(closest_val);
}
};