[Q] [Easy]
Given an integer x, return true if x is a palindrome, and false otherwise.
- int -> list 바꾸는 과정이 필요했다.
- 음수인 경우와 0보다 크고 10보다 작은 일의 자리 숫자인 경우도 따로 처리해줬다.
- 제출하다보니 예외가 계속해서 나와서 그걸 처리하다 보니 코드가 좀 지저분해졌다.
# Answer
import math
class Solution:
def isPalindrome(self, x: int) -> bool:
# 음수 따로 처리
if x < 0:
return False
elif (x >= 0) & (x < 10):
return True
else:
x_list = list(map(int, str(x)))
n = len(x_list)
middle = math.ceil(n/2)
if (middle > 1) & (x_list[:middle-1] == list(reversed(x_list[middle:]))):
answer = True
elif (middle == 1) | (n%2 == 0):
if x_list[:middle] == list(reversed(x_list[middle:])):
answer = True
else :
answer = False
else:
answer = False
return answer
'Algorithm > Leetcode' 카테고리의 다른 글
[Top Interview 150 - Math] 69. Sqrt(x) (1) | 2024.10.13 |
---|---|
[Top Interview 150 - Math] 172. Factorial Trailing Zeroes (1) | 2024.10.13 |
[Top Interview 150 - Hashmap] 128. Longest Consecutive Sequence (0) | 2024.10.13 |
[Top Interview 150 - Hashmap] 219. Contains Duplicate II (1) | 2024.10.12 |
[Top Interview 150 - Hashmap] 49. Group Anagrams (0) | 2024.10.12 |