Skip to content
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions submissions/Vratej-Dwivedi/Palindrome-Number/solution4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution(object):
def isPalindrome(self, n):
"""
:type n: int
:rtype: bool
"""
# Negative numbers and numbers ending in 0 (except 0 itself) are not palindromes
if n < 0 or (n % 10 == 0 and n != 0):
return False

rev_half = 0
while n > rev_half:
rev_half = rev_half * 10 + n % 10
n = n // 10

# For even length numbers: n == rev_half
# For odd length numbers: n == rev_half // 10
return n == rev_half or n == rev_half // 10

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions submissions/Vratej-Dwivedi/Valid-Palindrome/solution3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
def is_palindrome(sub):
return sub == sub[::-1]

l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
return is_palindrome(s[l+1:r+1]) or is_palindrome(s[l:r])
l += 1
r -= 1
return True