DEV Community

Shubham_Baghel
Shubham_Baghel

Posted on

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

Top comments (0)