less than 1 minute read

Problem

https://leetcode.com/problems/palindrome-number/submissions/

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

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Solution

My Original Solution

const isPalindrome = function(x) {
    x = x + ""
    let y = '';
    for (let i = x.length - 1; i >=0; i--){
        y += x[i];
    }
    return (x === y)
};

image-20230808010134944

I don’t think an explanation is needed.

Leave a comment