본문 바로가기
Coding Test

[CT] Leet Code 9. Palindrome Number

by Luciditas 2023. 9. 13.
728x90

9. Palindrome Number

Solved

 

Easy

 

Topics

 

Companies

 

Hint

Given an integer x, return true if x is a 

palindrome

 

, and false otherwise.

 

 

Example 1:

Input: x = 121

Output: true

Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121

Output: false

Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10

Output: false

Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

 

Constraints:

  • -231 <= x <= 231 - 1

 

Follow up: Could you solve it without converting the integer to a string?

Accepted

3.6M

 

Submissions

6.6M

 

Acceptance Rate

54.4%

 

나의 풀이

class Solution:
    def isPalindrome(self, x: int) -> bool:
        xstr = list(str(x))
        # 정수 x를 문자열로 바꾼 리스트를 만든다.
        return xstr == xstr[::-1]
        # 이 때 xstr과 xstr[::-1](xstr을 뒤집은 리스트)가 같으면 True, 다르면 False를 리턴한다.
728x90