//GitHub https://github.com/vellimole0621
(문제 - https://leetcode.com/problems/palindrome-number/ )
문제 설명 :
Given an integer x, return true if x is a palindrome, and false otherwise.
=> Palindrome 수, 앞 뒤 가 대칭인 수를 찾아라.
풀이 :
음수 인지 확인 > 홀수 짝수 경우의 수 나눔 > 반복문을 통해 대칭 확인 > 결과값 출력
class Solution:
def isPalindrome(self, x):
# Check is a boolean for x, Palindrome or not.
check = True
# 1. is x < 0??
if x < 0:
return False
else:
# x to int , to list
s_num = str(x)
sl_num = list(s_num)
mid_num = int(int(len(s_num))/2)
# 2-1. if x is odd num
if len(s_num)%2 != 0:
for n in range(0, mid_num):
if sl_num[mid_num - 1 - n] != sl_num[n + 1 + mid_num]:
check = False
break
# 2-2. if x is even num
else:
for n in range(0, mid_num):
if sl_num[mid_num - 1 - n] != sl_num[n + mid_num]:
check = False
break
if check:
return True
else:
return False
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Unique Paths (0) | 2023.07.16 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Single Number II (0) | 2023.07.04 |
[파이썬 Python/알고리즘] LeetCode - Buddy Strings (0) | 2023.07.03 |
[파이썬 Python/알고리즘] LeetCode - Two Sum (0) | 2023.06.30 |
[Python] 블랙잭 게임 (0) | 2023.02.26 |