简体   繁体   中英

Finding next largest palindrome of a number

I was trying to solve this problem for the next largest palindrome on SPOJ but it is throwing me a Time Limit Exceeded error. This is my approach for the problem in python,

t = int(raw_input().strip())

for i in range(t):
    a = raw_input()
    a = str(int(a) + 1)
    palin = ""

    if (len(a) % 2 == 0):

        reverseoffirst = []
        mainStr = a

        firsthalf = mainStr[0:len(a) / 2]
        secondhalf = firsthalf[::-1]

        palin = "".join(firsthalf) + "".join(secondhalf)

        if (int(palin) < int(a)):
            firsthalf = str(int(firsthalf) + 1)
            secondhalf = firsthalf[::-1]
            palin = "".join(firsthalf) + "".join(secondhalf)


    else:
        median = len(a) / 2
        mainStr = a

        if(median == 0):
            palin = "11"

        else:
            firsthalf = mainStr[0:median]
            secondhalf = firsthalf[::-1]

            palin = "".join(firsthalf) + mainStr[median] + "".join(secondhalf)

            if (int(palin) < int(a)):
                lastvalue = int(mainStr[median]) + 1

                if (lastvalue == 10):
                    firsthalf = str(int(firsthalf) + 1)
                    secondhalf = firsthalf[::-1]
                    palin = firsthalf + "0" + secondhalf

                else:
                    palin = firsthalf + str(lastvalue) + secondhalf
    print palin

I know that my question might get downvoted for being duplicate of several questions like this but my approach is not copied from anywhere. That is why i wanted to know what is wrong with my code.

Please if you can point out any kind of way that this code can get any better or any testcase that it does not satisfy or any other approach that would be very helpfull for me. Thank you!!

I took your code, and changed it a bit so your logic is the same.

Try to debug it so you will understand the way it work better.

Pay attention that it will work only for number and not letters.

Basically, according to your logic, I checked that the join of the first half with the second half is the next palindrome

Good luck

a = str(int(input())) # the initial number to check
is_palin = False
while True:
    a = str(int(a) + 1)
    if is_palin:
        print palin
        break
    palin = ""

    if (len(a) % 2 == 0):

        reverseoffirst = []
        mainStr = a

        firsthalf = mainStr[0:len(a) / 2]
        secondhalf = firsthalf[::-1]

        palin = "".join(firsthalf) + "".join(secondhalf)

        if (int(palin) == int(a)):
            is_palin = True
            print palin


    else:
        median = (len(a) / 2)
        mainStr = a
        firsthalf = mainStr[0:median]
        secondhalf = firsthalf[::-1]
        palin = "".join(firsthalf) + mainStr[median] + "".join(secondhalf)
        if (int(palin) == int(a)):
            is_palin = True
            print palin

输出量

Perhaps not the most efficient method ever:

def IsPalindrome(n):
       s = str(n)
       l = len(s)
       return s[:l/2] == s[:(l+1)/2-1:-1]

def NextPalindrome(n):
       while not IsPalindrome(n):
              n += 1
       return n

But NextPalindrome(65973) returns 66066 instantly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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