본문 바로가기
Python/Algorithm

[LeetCode] 9. Palindrome Number (Easy)

by jiyoon_92 2022. 9. 8.
반응형

LeetCode

https://leetcode.com/problems/palindrome-number/

 

Palindrome Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

Palindrome Number 란?

회문(回文) 또는 팰린드롬(palindrome)은 거꾸로 읽어도 제대로 읽는 것과 같은 문장이나 낱말, 숫자, 문자열(sequence of characters) 등이다. 보통 낱말 사이에 있는 띄어쓰기나 문장 부호는 무시한다.


문제

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

(int형 숫자 x를 받아서 palindrome number 인 경우 true를 리턴하라.)

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

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

코드

class Solution:
    def isPalindrome(self, x: int) -> bool:
        t = str(x)
        t2 = t[::-1]
        for a, b in zip(t, t2) :
            if (a != b) :
                return False
        return True

 


결과

반응형

'Python > Algorithm' 카테고리의 다른 글

[Programmers] 체육복 (Python)  (0) 2022.06.23
[Programmers] 숫자 문자열과 영단어 (Python)  (2) 2022.06.09

댓글