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) {
        return "";
    }

}

@@ -533,17 +533,1439 @@
     
-return %22%22
+if (n.length() %3E= 2 && allNine(n)) %7B%0A            String s = %221%22;%0A            for (int i = 0; i %3C n.length() - 1; i++) %7B%0A                s += %220%22;%0A            %7D%0A            s += %221%22;%0A            return s;%0A        %7D%0A        boolean isOdd = (n.length() %25 2 != 0);%0A        String left = n.substring(0, (n.length() + 1) / 2);%0A        long%5B%5D increment = %7B-1, 0, +1%7D;%0A        String ret = n;%0A        long minDiff = Long.MAX_VALUE;%0A        for (long i : increment) %7B%0A            String s = getPalindrom(Long.toString(Long.parseLong(left) + i), isOdd);%0A            if (n.length() %3E= 2 && (s.length() != n.length() %7C%7C Long.parseLong(s) == 0)) %7B%0A                s = %22%22;%0A                for (int j = 0; j %3C n.length() - 1; j++) %7B%0A                    s += %229%22;%0A                %7D%0A            %7D%0A            long diff = s.equals(n) ? Long.MAX_VALUE : Math.abs(Long.parseLong(s) - Long.parseLong(n));%0A            if (diff %3C minDiff) %7B%0A                minDiff = diff;%0A                ret = s;%0A            %7D%0A        %7D%0A        return ret;%0A    %7D%0A%0A    private String getPalindrom(String s, boolean isOdd) %7B%0A        String right = new StringBuilder(s).reverse().toString();%0A        return isOdd ? s.substring(0, s.length() - 1) + right : s + right;%0A    %7D%0A%0A    private boolean allNine(String s) %7B%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            if (s.charAt(i) != '9') %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true
 ;%0A  

edit 564