码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 564. Find the Closest Palindrome

时间:2020-01-05 09:47:45      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:||   could   lin   href   col   als   zed   its   clu   

原题链接在这里: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

标签:||   could   lin   href   col   als   zed   its   clu   

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12151375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!