DEV Community

Shubham_Baghel
Shubham_Baghel

Posted on

1

LeetCode Palindrome Number Solution in JavaScript

LeetCode:
Palindrome Number

Problem statement:
Given an integer x, return true if x is a
palindrome, and false otherwise.

Explanation:
Two pointer approach is best here as we can traverse string from one pointer and decrease it from end of string and check if its same or not

 /**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
//convert integer to string
    const convertedString=x.toString();
//create left pointer and right pointer
    let left=0;
    let right=convertedString.length-1;
//iterate till condition meets
    while(left<=right){
//check left string is similar to right or not
        if(convertedString[left]!=convertedString[right]){
           return false;
        }
//increase left pointer and decrease right pointer
         left++;
         right--;
    }
    return true;
};
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay