[LeetCode] Palindrome Number

동스토리 ㅣ 2021. 8. 7. 17:46

반응형

안녕하세요.

 

LeetCode 9번 Palindrome Number 문제풀이 하겠습니다.

 

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

[문제]

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.

-> 역순이 되어도 같은 수가 되는 경우

 

[TestCase]

[해설]

x를 문자열 리스트로 변경 후 Slicing 기법으로 문제를 해결했습니다.

>> arr[::-1] # 처음부터 끝까지 -1칸 간격으로 ( == 역순으로)

 

[코드]

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = list(str(x))
        
        return x == x[::-1]

 

문자열로 변경해서 문제를 풀면 간단하게 풀 수 있지만, 문자열로 변경하지 않고 풀어보면 더 좋을 거 같습니다.

 

감사합니다.

반응형

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

[LeetCode] Merge Two Sorted Lists  (0) 2021.08.17
[LeetCode] Roman to Integer  (0) 2021.08.08
[LeetCode] Reverse Integer  (0) 2021.08.06
[LeetCode] Add Two Numbers  (0) 2021.08.04
[LeetCode] Two Sum  (0) 2021.08.02