Palindrome Number

9. Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Solution:
convert int to string, and reverse it to check whether palindrome

def isPalindrome(x):
    """
    :type x: int
    :rtype: bool
    """       
    sign = 1 if x>0 else -1
    x = abs(x)
    reverse = str(x)[::-1]   
    return sign * int(reverse) == x

Question:
check whether string is a palindrome

Solution:
use two pointer to solve

def isPalindrome(x):
    """
    :type x: int
    :rtype: bool
    """  
    s, e = 0, len(x)-1
    flag = True
    while s < e:
        if a[s] != a[e]:
            flag = False
            break
        else:
            s += 1
            e -= 1
# python

Commentaires

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×