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)
  • 相关阅读:
    前端学PHP之错误处理
    mysql数据库学习目录
    前端学数据库之存储
    前端学数据库之函数
    用shell脚本监控进程是否存在 不存在则启动的实例
    在notepad++里面使用正则表达式替换掉所有行逗号前面内容
    mysql合并 两个count语句一次性输出结果的方法
    硬件中断和DPC一直占40-52%左右 解决方法
    解决secureCRT 数据库里没有找到防火墙 '无' 此会话降尝试不通过防火墙进行连接。
    Java eclipse下 Ant build.xml实例详解 附完整项目源码
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10753552.html
Copyright © 2011-2022 走看看