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)
  • 相关阅读:
    ORA-04013,CACHE 值必须小于CYCLE值;解决方案
    .net 调用WCF服务接收数据对象属性为空
    各JAVA JDK版本下载地址
    记录一次基于Echart的数据可视化平台开发
    Swiper轮播使用记录--一个页面有多个DIV区域使用swiper进行轮播
    Echart使用记录-动态设置series的时候,设置了color属性,但所有的柱状图显示灰白色
    记录一次SignalR服务的搭建注意事项
    C#关于ListBox绑定list,不会刷新数据的问题
    关于bootstrap-fileinput上传后删除选中上传的文件,回到最初的选择文件界面
    Oracle 数据库 alert日志及trace日志的清理
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10753552.html
Copyright © 2011-2022 走看看