Palindrome Number
문제 설명
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
- For example, 121 is a palindrome while 123 is not.
제한 사항
- -231 <= x <= 231 - 1
입출력 예
x | return |
121 | True |
-121 | False |
10 | False |
Python 코드
- int를 문자열로 변환
135ms code Python
class Solution: def isPalindrome(self, x: int) -> bool: return str(x) == str(x)[::-1]
56ms code Python
class Solution: def isPalindrome(self, x: int) -> bool: return False if x < 0 else str(x) == str(x)[::-1]
* 참고 링크 1 : https://hackmd.io/@y56/Hk6PWb7mr?type=view
* 참고 링크 2 : https://dhairya2136.medium.com/leetcode-9-palindrome-number-python-bdcd99bd7068
* Python Array [ :: ] 용법
arr[::], arr[1:2:3], arr[::-1] 등으로 배열의 index에 접근하는 방법을 Extended Slices(확장형 슬라이스)라고 한다.
arr[A:B:C]의 의미는, index A 부터 index B 까지 C의 간격으로 배열을 만들어라는 말입니다.
- A가 None 이라면, 처음부터 라는 뜻
- B가 None 이라면, 할 수 있는데까지
- C가 양수라면 마지막 index까지, C가 음수라면 첫 index까지가 된다.
- 마지막으로 C가 None 이라면 한 칸 간격으로 라는 뜻
arr = range(10) arr # result : [0,1,2,3,4,5,6,7,8,9] arr[::2] # 처음부터 끝까지 두 칸 간격으로 # result : [0,2,4,6,8] arr[1::2] # index 1 부터 끝까지 두 칸 간격으로 # result : [1,3,5,7,9] arr[::-1] # 처음부터 끝까지 -1칸 간격으로 ( == 역순으로) # result : [9,8,7,6,5,4,3,2,1,0] arr[::-2] # 처음부터 끝까지 -2칸 간격으로 ( == 역순, 두 칸 간격으로) # result : [9,7,5,3,1] arr[3::-1] # index 3 부터 끝까지 -1칸 간격으로 ( == 역순으로) # result : [3,2,1,0] arr[1:6:2] # index 1 부터 index 6 까지 두 칸 간격으로 # result : [1,3,5]
* 참고 링크 1 : https://blog.wonkyunglee.io/3
* 참고 링크 2 : https://docs.python.org/release/2.3.5/whatsnew/section-slices.html
* is_palindrome ( ) 함수 : 회문 판별 함수
palindrome(회문)은 순서를 거꾸로 읽어도 제대로 읽은 것과 같은 단어와 문장을 의미한다.
예를 들어 "level", "SOS", "rotator", "nurses run"과 같은 단어와 문장이 있다.
def is_palindrome(word): return word[::-1]==word # True print( is_palindrome("kayak") ) print( is_palindrome("madam") ) print( is_palindrome("racecar") ) print( is_palindrome("abradacadarba") ) print( is_palindrome("토마토") ) # False print( is_palindrome('hello') ) print( is_palindrome('coffee') )
* 참고 링크 1 : https://zetawiki.com/wiki/%ED%95%A8%EC%88%98_is_palindrome()
* 참고 링크 2 : https://dojang.io/mod/page/view.php?id=2331
C++ 코드
class Solution { public: bool isPalindrome(int x) { string temp = std::to_string(x); int left = 0; int right = temp.size()-1; while(left<=right) { if(temp[left++]!=temp[right--]) return false; } return true; } };
C ++ 100% 0ms code
- String으로 바꾸지 않고 맨 뒤의 값과 맨 앞의 값을 비교하면서 푼 코드
class Solution { public: bool isPalindrome(int x) { int temp = x; long rev, rem; rev = 0; if (x < 0) return false; while (temp != 0){ rem = temp % 10; rev = rev * 10 + rem; temp = temp/10; } if (x == rev) return true; else return false; } };
* 참고 링크 : https://kkminseok.github.io/posts/leetcode_Palindrome_Number/
출처
https://leetcode.com/problems/palindrome-number/
'코딩테스트 > Leet Code' 카테고리의 다른 글
[Leet Code] 136. Single Number (0) | 2022.01.13 |
---|---|
[Leet Code] 118. Pascal's Triangle 다시 공부 ★ (0) | 2022.01.13 |
[Leet Code] 15. 3Sum 다시 공부 ★ (0) | 2022.01.13 |
[Leet Code] 2. Add Two Numbers 다시 공부 ★ (0) | 2022.01.13 |
[Leet Code] 100. Same Tree (0) | 2022.01.05 |