LeetCode Palindrome Number Solution Explained - Java

LeetCode Palindrome Number Solution Explained - Java

Nick White

5 лет назад

86,680 Просмотров

The Best Place To Learn Anything Coding Related - https://bit.ly/3MFZLIZ

Join my free exclusive community built to empower programmers! - https://www.skool.com/software-developer-academy

Preparing For Your Coding Interviews? Use These Resources
————————————————————
(My Course) Data Structures & Algorithms for Coding Interviews - https://thedailybyte.dev/courses/nick
AlgoCademy - https://algocademy.com/?referral=nickwhite
Daily Coding Interview Questions - http://bit.ly/3xw1Sqz

10% Off Of The Best Web Hosting! - https://hostinger.com/nickwhite

Follow My Twitter - https://twitter.com/nicholaswwhite
Follow My Instagram - https://www.instagram.com/nickwwhite

Other Social Media
----------------------------------------------
Discord - https://discord.gg/ZGyc2nZyAx
Twitch - https://www.twitch.tv/nickwhitettv
TikTok - https://www.tiktok.com/@nickwhitetiktok
LinkedIn - https://www.linkedin.com/in/nicholas-w-white/

Show Support
------------------------------------------------------------------------------
Patreon - https://www.patreon.com/nick_white
PayPal - https://paypal.me/nickwwhite?locale.x...
Become A Member - https://www.youtube.com/channel/UC1fLEeYICmo3O9cUsqIi7HA/join

#coding #programming #softwareengineering

Тэги:

#palindrome #integer #int #java #leetcode #problems #interview #coding #programming #algorithms #solutions #number #technology #science #computer_science
Ссылки и html тэги не поддерживаются


Комментарии:

Tanmay Singal
Tanmay Singal - 02.08.2023 19:51

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?

Ответить
MomentumBees
MomentumBees - 05.07.2023 09:58

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.");
}
}
}

Ответить
David Zenteno
David Zenteno - 02.04.2023 07:13

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?

Ответить
Sayli K
Sayli K - 09.03.2023 22:21

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

Ответить
Sourav Saha
Sourav Saha - 29.09.2022 22:19

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 😂.

Ответить
Sangram Gotke
Sangram Gotke - 14.07.2022 15:17

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;

Ответить
Khanyisani Ntabeni
Khanyisani Ntabeni - 05.06.2022 16:22

run time is high though

Ответить
Kalahari
Kalahari - 27.03.2022 03:35

Smiling... 😪 your current facial expressions in videos say alot *sigh

Ответить
Mannan Qazi
Mannan Qazi - 23.03.2022 10:44

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.

Ответить
Mohammed Ghabyen
Mohammed Ghabyen - 22.03.2022 14:42

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;

Ответить
Siva Ayyappa Kumar Buddi
Siva Ayyappa Kumar Buddi - 15.03.2022 18:55

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

Ответить
Daily Prophet⚡
Daily Prophet⚡ - 11.03.2022 18:17

Line 20 x== reversed/10 why?

Ответить
TF H
TF H - 12.02.2022 01:19

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

Ответить
Krypton
Krypton - 30.01.2022 05:58

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.

Ответить
Leonard.
Leonard. - 29.11.2021 06:23

ur bad

Ответить
manco82
manco82 - 10.10.2021 22:52

This is a real bullshit exercise, requires mathematical understanding that is 99% irrelevant in real world programming jobs.

Ответить
Akhila
Akhila - 17.08.2021 09:13

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

Ответить
Alpacas
Alpacas - 18.06.2021 21:45

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

Ответить
Kelly Wilbert
Kelly Wilbert - 13.01.2021 03:35

Love you.

Ответить
Rupal Desai
Rupal Desai - 14.08.2020 03:50

Why the x%10==0?

Ответить
Shauna Hyppolite
Shauna Hyppolite - 13.05.2020 21:15

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

Ответить
Smith Codes
Smith Codes - 11.04.2020 03:17

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.

Ответить
iTech
iTech - 19.12.2019 20:24

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
iTech - 13.12.2019 06:08

wp Nick! Liked!

Ответить
iTech
iTech - 13.12.2019 06:08

and i couldnt do that myself...............................

Ответить
Mohamed Sugal
Mohamed Sugal - 27.10.2019 23:36

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

Ответить