题目:
题解:
using ULL = unsigned long long;
class Solution {
public:
vector<ULL> getCandidates(const string& n) {
int len = n.length();
vector<ULL> candidates = {
(ULL)pow(10, len - 1) - 1,
(ULL)pow(10, len) + 1,
};
ULL selfPrefix = stoull(n.substr(0, (len + 1) / 2));
for (int i : {selfPrefix - 1, selfPrefix, selfPrefix + 1}) {
string prefix = to_string(i);
string candidate = prefix + string(prefix.rbegin() + (len & 1), prefix.rend());
candidates.push_back(stoull(candidate));
}
return candidates;
}
string nearestPalindromic(string n) {
ULL selfNumber = stoull(n), ans = -1;
const vector<ULL>& candidates = getCandidates(n);
for (auto& candidate : candidates) {
if (candidate != selfNumber) {
if (ans == -1 ||
llabs(candidate - selfNumber) < llabs(ans - selfNumber) ||
llabs(candidate - selfNumber) == llabs(ans - selfNumber) && candidate < ans) {
ans = candidate;
}
}
}
return to_string(ans);
}
};