forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_564.java
68 lines (57 loc) · 1.93 KB
/
_564.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.fishercoder.solutions;
/**
* 564. Find the Closest Palindrome
*
* Given an integer n, find the closest integer (not including itself), which is a palindrome.
The 'closest' is defined as absolute difference minimized between two integers.
Example 1:
Input: "123"
Output: "121"
Note:
The input n is a positive integer represented by string, whose length will not exceed 18.
If there is a tie, return the smaller one as answer.
*/
public class _564 {
public String nearestPalindromic(String n) {
if (n.length() >= 2 && allNine(n)) {
String s = "1";
for (int i = 0; i < n.length() - 1; i++) {
s += "0";
}
s += "1";
return s;
}
boolean isOdd = (n.length() % 2 != 0);
String left = n.substring(0, (n.length() + 1) / 2);
long[] increment = {-1, 0, +1};
String ret = n;
long minDiff = Long.MAX_VALUE;
for (long i : increment) {
String s = getPalindrom(Long.toString(Long.parseLong(left) + i), isOdd);
if (n.length() >= 2 && (s.length() != n.length() || Long.parseLong(s) == 0)) {
s = "";
for (int j = 0; j < n.length() - 1; j++) {
s += "9";
}
}
long diff = s.equals(n) ? Long.MAX_VALUE : Math.abs(Long.parseLong(s) - Long.parseLong(n));
if (diff < minDiff) {
minDiff = diff;
ret = s;
}
}
return ret;
}
private String getPalindrom(String s, boolean isOdd) {
String right = new StringBuilder(s).reverse().toString();
return isOdd ? s.substring(0, s.length() - 1) + right : s + right;
}
private boolean allNine(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '9') {
return false;
}
}
return true;
}
}