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)
  • 相关阅读:
    Python深入03 对象的属性
    利用Webkit抓取动态网页和链接
    分享:OCILIB 3.11.0 发布,跨平台 Oracle 驱动
    Knockoutjs实战开发:控制子绑定(control descendant bindings)
    利用InjectedBundle定制自己的Webkit(二)
    使用solrj和EasyNet.Solr进行原子更新
    Chaos网络库(二) Buffer的设计
    分享:djangohaystack+solr实现搜索
    Moon.ORM 4.4 隆重发布,在性能和使用便捷上一挑群雄(mysoft,cyq,pdf)
    数据结构利器之私房STL(中)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10753552.html
Copyright © 2011-2022 走看看