-
Notifications
You must be signed in to change notification settings - Fork 4
/
FindTheClosestPalindrome.java
34 lines (31 loc) · 1.03 KB
/
FindTheClosestPalindrome.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
30
31
32
33
34
package com.dbc;
import java.util.ArrayList;
import java.util.List;
public class FindTheClosestPalindrome {
public String nearestPalindromic(String n) {
int length = n.length();
long int_n = Long.parseLong(n);
long prefix = Long.parseLong(n.substring(0, (length + 1) / 2));
List<Long> candidate = new ArrayList<>();
candidate.add((long) (Math.pow(10, (length - 1)) - 1));
candidate.add((long) (Math.pow(10, length) + 1));
for (long i = prefix - 1; i < prefix + 2; i++) {
long y = length % 2 == 0 ? i : i / 10;
long x = i;
while (y != 0) {
x = x * 10 + y % 10;
y /= 10;
}
candidate.add(x);
}
long res = -1;
for (long candi : candidate) {
if (candi - int_n != 0) {
if (res == -1 || Math.abs(int_n - candi) < Math.abs(int_n - res)) {
res = candi;
}
}
}
return Long.toString(res);
}
}