리트코드 > Palindrome Number

Doyeon Kim·2022년 3월 7일

코딩테스트 공부

목록 보기
28/171

문제 링크 : https://leetcode.com/problems/palindrome-number/


맨 처음에 반을 정해서 이분탐색..?을 할까 생각하다 실패..
그리고 생각(찾은)방법이 x를 거꾸로 한 값이 x와 같은지 확인한다

class Solution:
    def isPalindrome(self, x: int) -> bool:
        rev = list(reversed(x))
        if rev == x:
            return True
        else:
            return false

결과 :

TypeError: 'int' object is not reversible
    rev = list(reversed(x))
Line 3 in isPalindrome (Solution.py)
    ret = Solution().isPalindrome(param_1)
Line 26 in _driver (Solution.py)
    _driver()
Line 37 in <module> (Solution.py)

이후 수정된 코드

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

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

그.런.데?

Follow up: Could you solve it without converting the integer to a string?

ㄴfuck you
.
.

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x == 0: 
            return True 
        if x < 0: 
            return False 
        num = [] 
        while x!=0: 
            x, y = x//10, x%10 
            num.append(y) 
        return num == num[::-1]

x =10
num = [] 
while x!=0: 
  x, y = x//10, x%10 
  num.append(y)
print(num)

숫자값을 이용하여(?) 리버스를 만든다.
-> 프린트를 하면 [0,1]이 나온다

profile
성장하고 도전하는 개발자. 프로그래밍 좋아하세요?

0개의 댓글