https://leetcode.com/problems/palindrome-number/

 

 

주어진 숫자가 팰린드롬을 충족하는지 확인한다.

직관적으로, 두 개의 포인터를 생성하여 비교하는 방법을 이용할 수 있겠다.

첫번째 포인터는 왼쪽에서부터 오른쪽으로,

두번째 포인터는 오른쪽에서 왼쪽으로 순회한다.

그리고 그 포인터가 가리키는 값이 다르면 곧바로 False 를 출력한다.

음수는 minus 부호 때문에 팰린드롬을 충족하지 않는다.

따라서 곧바로 False 출력한다.

 

 

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0 : return False
        x = str(x)
        mid = int(len(x)/2)
        for i in range(0,mid):
            l = x[i]
            r = x[-i-1]
            if l!=r: return False
        return True

 

 

이 방법 외에, 숫자값을 뒤집은 새로운 숫자를 만들어서 비교하는 방법도 있겠다.

 

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0 : return False
        a = str(x)
        b = a[::-1]
        return a==b

 

 

 

 


 

< English (feedback by ChatGPT) >

 

 

 

Task is to check if the given number is satisfied Palindrome or not.

(The task is to check whether the given number is a palindrome.)

 

Intuitionally, we can use two pointers to compare.

(Intuitively, we can use two pointers to compare the digits.)

 

First pointer iterates from left to right

(The first pointer iterates from left to right,)

 

Second pointer iterates from right to left.

(and the second pointer iterates from right to left.)

 

And return False when the two pointer's values are not matched.

(If the values pointed to by the two pointers do not match, we immediately return False.)

 

Minus number has '-' mark so it can't be Palindrome.

(Negative numbers are not palindromes because of the minus sign (-),)

 

So return False.

(so we can return False right away.)

 

Another solution is to compare a new reverse number of the given number.

(Alternatively, we can reverse the number and compare it with the original.)








 

 

 

 

 

'Coding Interview' 카테고리의 다른 글

[leetcode] 20. Valid Parentheses  (0) 2025.05.01
[leetcode] 14. Longest Common Prefix  (1) 2025.05.01
[leetcode] 1. Two Sum  (0) 2025.04.29
[IT] CS 면접 대비 OS 질문 모음  (0) 2022.01.07
[IT] CS 면접 대비 JAVA 질문 모음  (0) 2021.12.31

+ Recent posts