[leetcode | 파이썬] Palindrome Number

devheyrin·2022년 1월 3일
0

codingtest

목록 보기
5/65
post-thumbnail

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 a palindrome while 123 is not.

  • 한국어 ver. 정수 x가 주어질 때, x가 '회문' 정수인 경우 True를 반환한다. 정수는 정방향과 역방향이 같을 때 '회문'이라고 한다. 예를 들어, 121은 회문이지만 123 은 회문이 아니다.

Example

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Solution

class Solution:
    def isPalindrome(self, x: int) -> bool:
        result = ''
        for i in reversed(str(x)):
            result += i
        if result == str(x):
            return True
        else:
            return False
  • 풀이 설명
    1. 정수x를 string으로 바꾼 뒤, 시퀀스 객체에 적용할 수 있는 reversed함수를 사용해 좌우반전시킨다.
    2. 좌우반전시킨 객체를 하나씩 돌면서 result라는 빈 스트링에 하나씩 추가해 좌우반전된 스트링 객체를 생성한다.
    3. 좌우반전된 string 이 정방향의 string과 일치하는지 확인하여 일치하는 경우 True를 반환한다.
profile
개발자 헤이린

0개의 댓글