Juni_DEV

[Python, LeetCode] 9. Palindrome Number 본문

Coding Interview

[Python, LeetCode] 9. Palindrome Number

junni :p 2023. 4. 14. 11:08
반응형
 

Palindrome Number - LeetCode

Can you solve this real interview question? Palindrome Number - Given an integer x, return true if x is a palindrome, and false otherwise.   Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Ex

leetcode.com

주어진 문제

더보기

Given an integer x, return true if x is a palindrome, and false otherwise.


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:

  • -2^(31) <= x <= 2^(31) - 1

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

 

파이썬 리스트 뒤집기

  1. 슬라이싱 [::-1]
  2. .reverse() 메서드
  3. 내장함수 list(reversed())

풀이

def isPalindrome(self, x: int) -> bool:
        # 배열 뒤에서부터 세기
        # if str(x)[::-1] == str(x) : 
        #     return ('true')
        return str(x)[::-1] == str(x)

 

 

GitHub - SVB-algorithm-study/JuniPark: SVB

SVB. Contribute to SVB-algorithm-study/JuniPark development by creating an account on GitHub.

github.com

 

참고 :
https://codetorial.net/tips_and_examples/reverse_python_list_or_numpy_array.html

반응형
Comments