Java | Leetcode Java题解之第564题寻找最近的回文数
题目:
题解:
class Solution {
public String nearestPalindromic(String n) {
long selfNumber = Long.parseLong(n), ans = -1;
List<Long> candidates = getCandidates(n);
for (long candidate : candidates) {
if (candidate != selfNumber) {
if (ans == -1 ||
Math.abs(candidate - selfNumber) < Math.abs(ans - selfNumber) ||
Math.abs(candidate - selfNumber) == Math.abs(ans - selfNumber) && candidate < ans) {
ans = candidate;
}
}
}
return Long.toString(ans);
}
public List<Long> getCandidates(String n) {
int len = n.length();
List<Long> candidates = new ArrayList<Long>() {{
add((long) Math.pow(10, len - 1) - 1);
add((long) Math.pow(10, len) + 1);
}};
long selfPrefix = Long.parseLong(n.substring(0, (len + 1) / 2));
for (long i = selfPrefix - 1; i <= selfPrefix + 1; i++) {
StringBuffer sb = new StringBuffer();
String prefix = String.valueOf(i);
sb.append(prefix);
StringBuffer suffix = new StringBuffer(prefix).reverse();
sb.append(suffix.substring(len & 1));
String candidate = sb.toString();
try {
candidates.add(Long.parseLong(candidate));
} catch (NumberFormatException ex) {
continue;
}
}
return candidates;
}
}