C++ | Leetcode C++题解之第564题寻找最近的回文数
题目:
题解:
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);
}
};
原文地址:https://blog.csdn.net/Ddddddd_158/article/details/143826981
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!