Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1288 from Pho3b/patch-2
Browse files Browse the repository at this point in the history
Update: 9-Palindrome-Number.js
  • Loading branch information
Ahmad-A0 authored Oct 21, 2022
2 parents 471c6c4 + 0749b43 commit 677d705
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions javascript/9-Palindrome-Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,26 @@ var isPalindrome = function (x) {

// Runtime: 302 ms, faster than 40.50% of JavaScript online submissions for Palindrome Number.
// Memory Usage: 51.8 MB, less than 8.36% of JavaScript online submissions for Palindrome Number.

/**
* Reverse Integer Using Modulo
* Time O(N) | Space O(1)
* https://leetcode.com/problems/palindrome-number/
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x < 0) return false;

const inputX = x;
let revX = 0;

while (x > 0) {
revX += x % 10;
x = Math.floor(x / 10);

if (x > 0) revX *= 10
}

return revX === inputX;
};

0 comments on commit 677d705

Please sign in to comment.