[파이썬][Leetcode][릿코드] Palindrome Number
1. 문제Permalink
9. Palindrome Number
Easy
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 while123
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
Follow up: Could you solve it without converting the integer to a string?
출처: Leetcode, https://leetcode.com/
2. 해결방법 시간복잡도Permalink
- 단순 코딩 O(N)
3. 문제 해결 및 코드Permalink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def isPalindrome(self, x: int) -> bool: | |
tmp = [i for i in str(x)] | |
if tmp == list(reversed(tmp)): return True | |
# 뒤집어도 같다면 | |
else: return False |
-
주석을 참고하면서 이해를 돕습니다.Permalink
4. 알고리즘 및 해설Permalink
- 입력값을 문자열로 변경후 해당 문자들을 리스트로 만든다.
- 이후 해당 리스트와 해당 리스트를 뒤집은 값이 같은지 여부를 출력한다.
- reversed()를 통해 해당 리스트를 뒤집은다.
- 이때 해당 리스트로 변환하지 않으면 <list_reverseiterator object at 0xxxxxxxxxxxxx>와 같은 문구가 출력된다.