LeetCode 564. Find the Closest Palindrome

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 564. Find the Closest Palindrome相关的知识,希望对你有一定的参考价值。

原题链接在这里:https://leetcode.com/problems/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:

  1. The input n is a positive integer represented by string, whose length will not exceed 18.
  2. If there is a tie, return the smaller one as answer.

题解:

First find all the candidates, add "9999..999", "10000..0001".

Add first half, + reverse of (first half). 

e.g. 12567, its first half is 125, the candiates couldbe 12521, 12421, 12621.

Then check all the candidates and find the closest one.

Time Complexity: O(mn). m = is candidates list size. n is candiate length.

Space: O(mn).

AC Java:

 1 class Solution {
 2     public String nearestPalindromic(String n) {
 3         if(n == null || n.length() == 0){
 4             return n;
 5         }
 6         
 7         int len = n.length();
 8         if(len == 1){
 9             int val = Integer.valueOf(n);
10             return val > 0 ? "" + (val - 1) : "" + (val + 1);
11         }
12         
13         List<String> cans = new ArrayList<>();
14         
15         cans.add(allNine(len - 1));
16         cans.add(oneZero(len + 1));
17         
18         int halfLen = (len + 1) / 2;
19         String sub = n.substring(0, halfLen);
20         if(len % 2 == 1){
21             cans.add(sub + new StringBuilder(sub).deleteCharAt(halfLen - 1).reverse().toString());
22             long halfVal = Long.valueOf(sub);
23             long plusOne = halfVal + 1;
24             cans.add("" + plusOne + new StringBuilder("" + plusOne / 10).reverse().toString());
25             long minusOne = halfVal - 1;
26             if(minusOne > 0){
27                 cans.add( "" + minusOne + new StringBuilder("" + minusOne / 10).reverse().toString());
28             }
29         }else{
30             cans.add(sub + new StringBuilder(sub).reverse().toString());
31             long halfVal = Long.valueOf(sub);
32             long plusOne = halfVal + 1;
33             cans.add("" + plusOne + new StringBuilder("" + plusOne).reverse().toString());
34             long minusOne = halfVal - 1;
35             if(minusOne > 0){
36                 cans.add("" + minusOne + new StringBuilder("" + minusOne).reverse().toString());
37             }
38         }
39         
40         long diff = Long.MAX_VALUE;
41         String res = "";
42         long nValue = Long.valueOf(n);
43         for(String can : cans){
44             if(can.equals(n)){
45                 continue;
46             }
47             
48             long canValue = Long.valueOf(can);
49             if(Math.abs(canValue - nValue) < diff){
50                 diff = Math.abs(canValue - nValue);
51                 res = can;
52             }else if(Math.abs(canValue - nValue) == diff && (res.length() == 0 || canValue < Long.valueOf(res))){
53                 res = can;
54             }
55         }
56         
57         return res;
58     }
59     
60     private String allNine(int len){
61         char [] charArr = new char[len];
62         Arrays.fill(charArr, ‘9‘);
63         return new String(charArr);
64     }
65     
66     private String oneZero(int len){
67         char [] charArr = new char[len];
68         Arrays.fill(charArr, ‘0‘);
69         charArr[0] = ‘1‘;
70         charArr[len - 1] = ‘1‘;
71         return new String(charArr);
72     }
73 }

 

以上是关于LeetCode 564. Find the Closest Palindrome的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 564. Find the Closest Palindrome 锛堟瀯閫狅級

564. 寻找最近的回文数

#Leetcode# 997. Find the Town Judge

LeetCode: Find the Duplicate Number

LeetCode 997. Find the Town Judge

Leetcode: Find the Difference