Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
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.
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
class Solution:
def isPalindrome(self, x: int) -> bool:
strX = str(x)
for i in range(len(strX)//2+1):
if strX[i] == strX[len(strX)-1-i]:
continue
else:
return False
return True
먼저 for문을 이용해 중간까지만, 숫자가 데칼코마니가 되면 맞을거라고 생각했다.
그래서 어렵지 않게 풀이를 작성했다.
그러나 속도가 중간이하의 속도가 나와서 다시 공부를 해보기로 했다.
class Solution:
def isPalindrome(self, x: int) -> bool:
x=str(x)
if x==x[::-1] :
return True
else :
return False
이 코드는 무시무시하게 충격적인 코드였다. fot문을 돌리는게 아닌 단순히 x[::-1] 만 쓰면, reverse가 되는 거구나 하는 충격을 주었다.
진짜 간단하다.. 다음에 꼭 써먹어야겠다.