zoukankan      html  css  js  c++  java
  • 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:

    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.

    Approach #1: Logic. [Java]

    class Solution {
        public String nearestPalindromic(String n) {
            Long num = Long.parseLong(n);
            Long big = findHigherPalindrome(num+1);
            Long small = findLowerPalindrome(num-1);
            return Math.abs(num - small) > Math.abs(big - num) ? String.valueOf(big) : String.valueOf(small);
        }
        
        Long findHigherPalindrome(Long limit) {
            String n = Long.toString(limit);
            char[] s = n.toCharArray();
            int m = s.length;
            char[] t = Arrays.copyOf(s, m);
            for (int i = 0; i < m / 2; ++i) {
                t[m-i-1] = t[i];
            }
            for (int i = 0; i < m; ++i) {
                if (s[i] < t[i]) return Long.parseLong(String.valueOf(t));
                else if (s[i] > t[i]) {
                    for (int j = (m-1) / 2; j >= 0; --j) {
                        if (++t[j] > '9') t[j] = '0';
                        else break;
                    }
                    for (int k = 0; k < m / 2; ++k) {
                        t[m-1-k] = t[k];
                    }
                    return Long.parseLong(String.valueOf(t));
                }
            }
            return Long.parseLong(String.valueOf(t));
        }
        
        Long findLowerPalindrome(Long limit) {
            String n = Long.toString(limit);
            char[] s = n.toCharArray();
            int m = s.length;
            char[] t = Arrays.copyOf(s, m);
            for (int i = 0; i < m / 2; ++i) {
                t[m-1-i] = t[i];
            }
            for (int i = 0; i < m; ++i) {
                if (s[i] > t[i]) return Long.parseLong(String.valueOf(t));
                else if (s[i] < t[i]) {
                    for (int j = (m - 1) / 2; j >= 0; --j) {
                        if (--t[j] < '0') t[j] = '9';
                        else break;
                    }
                    if (t[0] == '0') {
                        char[] a = new char[m-1];
                        Arrays.fill(a, '9');
                        return Long.parseLong(String.valueOf(a));
                    }
                    for (int k = 0; k < m / 2; ++k) {
                        t[m-1-k] = t[k];
                    }
                    return Long.parseLong(String.valueOf(t));
                }
            }
            return Long.parseLong(String.valueOf(t));
        }
    }
    

      

    Analysis:

    We first need to find the higher palindrome and lower palidrome respectively. and return the one who has the least different with the input number.

    For the higher palindrome, the lower limit is number + 1 while for the lower palindrome, the hight limit is number - 1.

    One global solution to find a palindrome is to copy first half part of the array to the last half part, we regards this as standard palindrome.

    We need to detect this standard palindrome belongs to higher one or the lower one. And other solutions will be based on this standard one.

    For the higher palindrome, if the standard one belongs to higher, we just simply return it. Or we need to change it.

    For example, stirng n is 1343, and the standard palindrome is 1331. to get the higher one form the standard palidrome, we start from the first 3, which is (n.length - 1) / 2. Add the number by 1, 9--> 1431) if the added result is not higher than 9, the changing process is finished, otherwise, continuously changing the number of previous index by i. After the changing process, we re-palidrome the string. (1431 --> 1441)

    For the lower palidrom, similar idea. But we need to notice that when we decrease a number a number, and if the first character of the string is '0', we need to resize the array of n.length - 1, and fill in with '9'. (for example, n is '1000', the standard palidrome is '1001' (higher one), the lower one '0000' --> '999'.)

    Reference:

    https://leetcode.com/problems/find-the-closest-palindrome/discuss/102390/Java-solution-with-full-explaination

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    ASP.NET在MVC控制器中获取Form表单值的方法
    MVC中几种常用ActionResult
    EF 配置MySQL
    HTTP 错误 403.6
    26个Jquery使用小技巧(转)
    Win2008R2配置WebDeploy(转)
    IIS快捷方式
    发布你的程序包到Nuget
    PostgreSQL recovery.conf恢复配置
    PostgreSQL 9.5 高可用、负载均衡和复制
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10753552.html
Copyright © 2011-2022 走看看