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)
  • 相关阅读:
    bilibili安卓视频缓存生成mp4
    Java实现kmp算法,少量注释
    小程序MQTT、mqtt超简单的连接、附带Demo
    【STM32H7】第15章 ThreadX GUIX定时器更新功能
    【STM32F429】第15章 ThreadX GUIX定时器更新功能
    【STM32H7】第14章 GUIX Studio设计窗口切换
    【STM32F429】第14章 GUIX Studio设计窗口切换
    【STM32H7】第13章 ThreadX GUIX窗口任意位置绘制2D图形
    【STM32F429】第13章 ThreadX GUIX窗口任意位置绘制2D图形
    【STM32H7】第12章 GUIX Studio生成代码移植到硬件平台
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10753552.html
Copyright © 2011-2022 走看看