简体   繁体   English

最小回文数大于N

[英]The smallest Palindrome number is greater N

For a given positive integer N of not more than 1000000 digits, write the value of the smallest palindrome larger than N to output. 对于不超过1000000位数的给定正整数N,将大于N的最小回文的值写入输出。

Here is my code: 这是我的代码:

public class Palin {

    public static String reverseString(String s) {
        String newS = "";
        for(int i = s.length() - 1; i >= 0; i--)
            newS += s.charAt(i);
        return newS;
    }

    public static String getPalin(String s) {
        int lth = s.length();

        String left = "", mid = "", right = "", newS = "";
        if(lth % 2 != 0) {
            left = s.substring(0, lth / 2);
            mid = s.substring(lth / 2, lth / 2 + 1);
            right = reverseString(left);

            newS = left + mid + right;

            if(s.compareTo(newS) < 0) return newS;
            else {
                int temp = Integer.parseInt(mid);
                temp++;
                mid = Integer.toString(temp);
                newS = left + mid + right;
                return newS;
            }
        }
        else {
            left = s.substring(0, lth / 2 - 1);
            mid = s.substring(lth / 2 - 1, lth / 2);
            right = reverseString(left);

            newS = left + mid + mid + right;
            if(s.compareTo(newS) < 0) return newS;
            else {
                int temp = Integer.parseInt(mid);
                temp++;
                mid = Integer.toString(temp);
                newS = left + mid + mid + right;
                return newS;
            }
        }
    }

    public static void main(String[] args) throws java.lang.Exception {
        Scanner input = new Scanner(System.in);
        //Scanner input = new Scanner(System.in);

        int k = input.nextInt();
        String[] s = new String[k];

        for(int i = 0; i < k; i++) {
            s[i] = input.next();
        }

        for(int i = 0; i < k; i++) {
            System.out.println(getPalin(s[i]));
        }
    }
}

My idea is use a String represent for a number. 我的想法是使用String代表数字。 I divide this String into 2 part, coppy first part and reverse it for second part. 我将此String分为2部分,第一部分为coppy,第二部分为反向。 I think my solve is correct but it not fast enough. 我认为我的解决方法是正确的,但还不够快。 I need a more efficient algorithm. 我需要一个更高效的算法。 Thanks 谢谢

EDITED EDITED
Since you said that: 由于您说过:

For a given positive integer N of not more than 1000000 digits 对于给定的正整数N,其位数不超过1000000

My previous solution won't work since I have converted them to int and an int can't accommodate 1000000 digits . 我以前的解决方案无法正常工作,因为我已经将它们转换为int ,并且int无法容纳1000000位数字 Thus I have made a new approach, an approach that doesn't need any String to int conversion. 因此,我提出了一种新方法,该方法不需要任何String到int的转换。

Refer to the code and comment below for details. 有关详细信息,请参考下面的代码和注释。

CODE: 码:

package main;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // Scanner input = new Scanner(System.in);

        int k = Integer.parseInt(input.nextLine());
        String[] s = new String[k];

        for (int i = 0; i < k; i++) {
            s[i] = input.nextLine();
        }

        for (int i = 0; i < k; i++) {
            System.out.println(getPalin(s[i]));
        }

        input.close();

    }

    public static String getPalin(String s) {
        // initialize the result to "" since if input is 1 digit, nothing is printed
        String result = "";

        // if input is greater than 1000000 digits
        if (s.length() >= 1000000) {
            // return the highest palindrome less than 1000000
            result = "999999";
        } else if (s.length() > 1) {
            // get the middle index of the string
            int mid = s.length() % 2 == 0 ? s.length() / 2 : (s.length() / 2) + 1;

            // get the left part of the string
            String leftPart = getPalindrome(s.substring(0, mid));

            if (s.length() % 2 == 0) {
                // attach the left part and the reverse left part
                result = leftPart + new StringBuilder(leftPart).reverse().toString();
            } else {
                // attach the left part and the reverse left part excluding the middle digit
                result = leftPart
                        + new StringBuilder(leftPart.substring(0, leftPart.length() - 1)).reverse().toString();
            }

            // check if the new result greater than 1000000 digits
            if (result.length() >= 1000000) {
                // return the highest palindrome less than 1000000
                result = "999999";
            }
        }
        return result;
    }

    public static String getPalindrome(String param) {
        String result = "";

        // iterate through the string from last index until index 0
        for (int i = param.length() - 1; i >= 0; i--) {
            // get the char at index i
            char c = param.charAt(i);

            /*
             * increment char since the next palindrome is the current digit + 1. Example:
             * User input is 121, then param will be 12 so the next is 13
             */
            c++;

            /*
             * check if the current character is greater than '9', which means it is not a
             * digit after incrementing
             */
            if (c > '9') {
                // set the current char to 0
                c = '0';
                // check if index is at index 0
                if (i - 1 < 0) {
                    // if at index 0 then add '1' at start
                    result = '1' + result;
                } else {
                    // if not then append c at result
                    result = result + c;
                }
            } else {
                // check if index is at index 0
                if (i - 1 < 0) {
                    // if not then prepend c at result
                    result = c + result;
                } else {
                    // if not then get the rest of param then append c and result
                    result = param.substring(0, i) + c + result;
                }
                break;
            }
        }

        return result;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM