9. Palindrome Number

2021. 10. 27. 13:21Leetcode

Description:

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

 

'''
[1] math solution)

code::
    def isPalindrome(self, x: int) -> bool:
        if x<0:
            return False;
        
        src=x
        dst=0
        while x>0:
            dst=dst*10+(x%10)
            x//=10
            
        return src==dst

-T/C: O(log(x))
-S/C: O(1)
'''

'''
[2] using str() )

idea::
integer x -> str(x)
and reverse it and check reversed(str(x))==str(x)

code::
src=str(x)
dst="".join(reversed(src))

return src==dst

-T/C: O(len(str(x))) # due to reversed() and str.join()
-S/C: O(len(str(x)))
'''

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0:
            return False;
        
        src=x
        dst=0
        while x>0:
            dst=dst*10+(x%10)
            x//=10
            
        return src==dst

'Leetcode' 카테고리의 다른 글

75. Sort Colors  (0) 2021.10.27
234. Palindrome Linked List  (0) 2021.10.27
268. Missing Number  (0) 2021.10.27
338. Counting Bits  (0) 2021.10.27
278. First Bad Version  (0) 2021.10.26