Palindrome Number
Given an integer x, return true if x is a palindrome, and false otherwise.
Python
####Two Pointer (Runtime: 19ms, Memory: 12.3MB)
#DECLARE x: INTEGER
class Solution:
def isPalindrome(self, x):
y = str(x)
Head = 0
Tail = len(y) - 1
while Head < Tail:
if y[Head] != y[Tail]:
return False
Head += 1
Tail -= 1
return True
####Recursion (Runtime: 19ms, Memory: 12.3MB)
#DECLARE x: INTEGER
class Solution:
def isPalindrome(self, x):
y = str(x)
Length = len(y)
if y[0] != y[-1]:
return False
if Length <= 2:
return True
return self.isPalindrome(y[1:-1])
####Slicing Radius-Check(Runtime: 0ms, Memory: 12.3MB)
#DECLARE x: INTEGER
class Solution:
def isPalindrome(self, x):
y = str(x)
Radius = len(y) // 2
return y[:Radius] == y[::-1][:Radius]
####Mathematical Calculation (Runtime: 27ms, Memory: 12.3MB)
#DECLARE x: INTEGER
class Solution:
def isPalindrome(self, x):
if x < 0:
return False
Rev = 0
Temp = x
while Temp > 0:
Rev = Rev * 10 + (Temp%10)
Temp = Temp // 10
return x == Rev
####Mathematical Radius-Check (Runtime: 9ms, Memory: 12.4MB)
#DECLARE x: INTEGER
class Solution:
def isPalindrome(self, x):
if x < 0 or ((x%10==0) and (x!=0)):
return False
Temp = x
Rev = 0
while Temp > Rev:
Rev = Rev * 10 + (Temp%10)
Temp = Temp // 10
if Temp == Rev or Temp == (Rev//10):
return True
return False
Top comments (0)