Palindrome Number - Leetcode 9 - Python

preview_player
Показать описание


0:00 - Read the problem
1:10 - Drawing Explanation
8:23 - Coding Explanation

leetcode 9

#palindrome #number #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

Nice Solution!
We can write the divider value in this way as well 8:37:
if x==0: return True
div = 10**int(math.log10(x))

aspar
Автор

Great solution, thank you!

The solution implemented in c#:
public bool IsPalindrome(int x) {
if (x < 0)
{
return false;
}

long divider = 1;
while (divider * 10 <= x)
{
divider *= 10;
}

while (x > 0)
{
int l = (int) (x / divider);
int r = (int) (x % 10);

if (l != r)
{
return false;
}

// Cut the left digit
x = (int) (x % divider);

// Cut the right digit
x /= 10;

divider /= 100;
}

return true;
}

divider is in long type due to case the given x is one digit less the max int value.


Another solution, write the reserved number and compare it to the given x.
implemented in c#:
public bool IsPalindrome(int x) {
if (x < 0)
{
return false;
}

int reservedNumber = 0;
int originalX = x;

while (x > 0)
{
int lastDigit = x % 10;

reservedNumber = reservedNumber * 10 + lastDigit;
x /= 10;
}

return reservedNumber == originalX;
}

nisimkd
Автор

A simple solution using JS. The idea is to reverse the number and then compare it to the original one.

var isPalindrome = function(x) {
if (x < 0) return false;

let num = x;
let result = 0;

while (num > 0) {
result = result * 10 + (num % 10);

num = Math.floor(num / 10);
}

return result === x;
};

marioalfredojorge
Автор

watched this and did it using strings, gave myself a pat on the back.

anandsharma
Автор

Simple, efficient and straight to the point. Thanks for this answer.

babacarkane
Автор

I feel like this is a simpler approach:

public class Solution {
public bool IsPalindrome(int x) {
if(x < 0) return false;
if (x != 0 && x % 10 == 0) return false;

int original = x;
int reversed = 0;
while (x != 0) {
reversed = reversed * 10 + x % 10;
x /= 10;
}

return original == reversed;
}
}

kaankahveci
Автор

This was my C# solution (which is similar to yours) that I submitted a while ago;

if(x == 0)
return true;
if(x < 0 || x % 10 == 0)
return false;

var reverse = 0;

while(x > reverse)
{
var lastDigit = x % 10;
x /= 10;
reverse = reverse * 10 + lastDigit;
}

return x == reverse || x == reverse / 10;

Автор

I think this video may need a remake because it will not work when the input is 1410110141 on Java. I tried this and it worked:
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
} else {
int original = x, reversed = 0;
while (original > reversed) {
reversed = (reversed * 10) + (original % 10);
original /= 10;
}
return original == reversed || original == reversed / 10;
}

iforcollege
Автор

you could use decimal log for finding the divider

pavel.pavlov
Автор

Thank you for the solution! But I’m struggling to understand why we should chop off the left and right digits, couldn’t we just compare then and if they are equal return True? Please if someone could explain, I would be much grateful!

zerkinanastya
Автор

Damm. I created a linked list and rebuilt the number in reverse and then compared it. And that too in C. 😭😭

rehatsingh
Автор

A rather contrived problem. Not a fan. Great explanation though!

TheElementFive
Автор

Do you know how to fix this code to work with 0’s on both ends, say you end up with 0110, I think the computer will ignore the first 0 so it’ll try to match 1 and 0 which will return false, idk if this happens on python but i’m doing it in java

metawrld_dream
Автор

class Solution:
def isPalindrome(self, x: int) -> bool:
list1=[]
import math
y = [a for a in str(x)]
for i in range(0, math.floor(len(y)/2)+1):
if y[i]!=y[-i-1]:
list1.append(1)
else:
list1.append(0)
if 1 in list1:
return False
return True

ChangKaiHua
Автор

s = str(x)
return s == s[::-1]

This code is more effective and very simple to understand. Hope it was useful

bennychristiyan
Автор

class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]

gumogumo
Автор

Easiest Way Ever :
def isPalindrome(self, x):

num = x
reverse = 0
while(x>0):
lastdigit = x%10
reverse = reverse*10+lastdigit
x = x//10

return True if reverse == num else False

KumarMukand-dmiw
Автор

This solution will fail if the number contains "0" in the middle of a number right? Like 1000021

Xuze
Автор

Will this solution work for this testcase?

1000021

Because, once we remove the first and the last letter and proceed to the next step,
we have
000021 which is evaluated as 21

prabinlamsal
Автор

why do you assume 21/10 rounds down to 2?

sangpark