LeetCode Palindrome Number Solution Explained - Java

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


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

This is the first time I am coming across such a beautiful solution of Palindrome Number

akhila
Автор

For those who didn't understand why x%10 === 0 is checked in Line 7:
Try a dry run with test case x=10 or x as a multiple of 10. The x and rev values will be reversed from what is expected.

krypton
Автор

That is an elegant solution. What I used to do was just reverse the string array and compare those two arrays. The run time obviously was pretty bad.

smithcodes
Автор

Line 3 (correction) : if (x>=0 && x<=9){return true;}

saylik
Автор

public boolean isPalindrome(int x) {
String str = Integer.toString(x);
int left = 0;
int right = str.length() - 1;

while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}

return true;
}
This is much more cleaner

beka
Автор

Hey nick why did you check in the second if statement if (x < 0 || x % 10 == 0) why is the x % 10 needed here?

msugal
Автор

string intStr = x.ToString();
int left = 0;
int right = intStr.Length-1;

while(left<right){


return false;
left++;
right--;
}
return true;

mohammedghabyen
Автор

Won't reversing integers this way be susceptible to integer overflows? Like what if the original number is less that the MAX_INT but when you reverse it, it goes over MAX_INT value?

tanmaysingal
Автор

public class PalindromeNumber {
public static boolean isPalindrome(int number) {
if (number < 0) {
return false; // Negative numbers cannot be palindromes
}

int reversedNumber = 0;
int originalNumber = number;

while (number > 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}

return originalNumber == reversedNumber;
}

public static void main(String[] args) {
int number = 12321;

if (isPalindrome(number)) {
System.out.println(number + " is a palindrome number.");
} else {
System.out.println(number + " is not a palindrome number.");
}
}
}

momentumbees
Автор

Try to explain well, I mean for the base cases that u take in the program. No hurry /''you know''. There are lot of people who didn't get there answers 😂.

souravsaha
Автор

going from edabit very easy to leetcode easy is like going from hell to mt everest omg.

tfh
Автор

No presented solution before coding so it is completely not understandable what's going on.

exactly
Автор

Isn’t this still checking the entire int since it’s reversing the number and comparing if they equal once x <= reversed_int?

shaunhyp
Автор

I don't understand x == reversed_int/10 included in the if statement apart from x == reversed_int(Line 20)

sivaayyappakumarbuddi
Автор

so reversed_int is 0 on line 11, on line 16 will it always be (0 * 10) + pop? I am confused on this part. Won't reversed_int always equal pop?

davidzenteno
Автор

you are comparing( x== reversed_int); how this is equal mann?here reversed _int is half of the X, or always less, , think of this line20;

sangramgotke
Автор

unfortunately, you did not explain anything but read through the entire script. not very helpful. very disappointing.

wenkaiyang
Автор

does anyone know why he set while (x > reverse_int) instead of (x != 0)? at first I thought it would just be faster but I tried it both ways and x != 0 doesn't even work

alpacas
Автор

that does not look like an easy problem tbh

RaymondStormblssed
Автор

x%10 == 0 doesn't always work, for x = 0 for example it returns 0 but 0 is a palindrome (at least if you consider that single digit nums are palindromes) no?

itech