forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
29 lines (28 loc) · 988 Bytes
/
Solution.java
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
class Solution {
public String nearestPalindromic(String n) {
long x = Long.parseLong(n);
long ans = -1;
for (long t : get(n)) {
if (ans == -1 || Math.abs(t - x) < Math.abs(ans - x)
|| (Math.abs(t - x) == Math.abs(ans - x) && t < ans)) {
ans = t;
}
}
return Long.toString(ans);
}
private Set<Long> get(String n) {
int l = n.length();
Set<Long> res = new HashSet<>();
res.add((long) Math.pow(10, l - 1) - 1);
res.add((long) Math.pow(10, l) + 1);
long left = Long.parseLong(n.substring(0, (l + 1) / 2));
for (long i = left - 1; i <= left + 1; ++i) {
StringBuilder sb = new StringBuilder();
sb.append(i);
sb.append(new StringBuilder(i + "").reverse().substring(l & 1));
res.add(Long.parseLong(sb.toString()));
}
res.remove(Long.parseLong(n));
return res;
}
}