Тэги:
#palindrome #integer #int #java #leetcode #problems #interview #coding #programming #algorithms #solutions #number #technology #science #computer_scienceКомментарии:
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?
Ответить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.");
}
}
}
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?
ОтветитьLine 3 (correction) : if (x>=0 && x<=9){return true;}
Ответить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 😂.
Ответить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;
Ответитьrun time is high though
ОтветитьSmiling... 😪 your current facial expressions in videos say alot *sigh
ОтветитьQuick question, and please don't kill me for it. On Line 20, hasn't x been modified in the while loop multiple times? while loop terminated when x == 0. So how does Line 20 x == reversed_int hold true now? i.e. ( 0 == reversed_int)?
I'm a beginner so maybe I don't quite grasp the entire thing.
string intStr = x.ToString();
int left = 0;
int right = intStr.Length-1;
while(left<right){
if(intStr[left]!=intStr[right])
return false;
left++;
right--;
}
return true;
I don't understand x == reversed_int/10 included in the if statement apart from x == reversed_int(Line 20)
ОтветитьLine 20 x== reversed/10 why?
Ответитьgoing from edabit very easy to leetcode easy is like going from hell to mt everest omg.
Ответить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.
ur bad
ОтветитьThis is a real bullshit exercise, requires mathematical understanding that is 99% irrelevant in real world programming jobs.
ОтветитьThis is the first time I am coming across such a beautiful solution of Palindrome Number
Ответить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
ОтветитьLove you.
ОтветитьWhy the x%10==0?
ОтветитьIsn’t this still checking the entire int since it’s reversing the number and comparing if they equal once x <= reversed_int?
Ответить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.
Ответить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?
Ответитьwp Nick! Liked!
Ответитьand i couldnt do that myself...............................
ОтветитьHey nick why did you check in the second if statement if (x < 0 || x % 10 == 0) why is the x % 10 needed here?
Ответить