Skip to content

Commit 16366a0

Browse files
palindrome number better approach with maths 0(log(n)100)
1 parent 475e6f8 commit 16366a0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

leetcode/PalindromeNumber.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function isPalindrome(x) {
2-
const s = Number(String(x).split("").reverse().join(""));
3-
return x === s;
2+
if (x < 0 || (x !== 0 && x % 10 == 0)) return false;
3+
4+
let reverse = 0;
5+
6+
while (x > reverse) {
7+
reverse = reverse * 10 + (x % 10);
8+
x = ~~(x / 10);
9+
}
10+
11+
return x === reverse || x === ~~(reverse / 10);
412
}
5-
//My first iteration over Palindrome number, quadratic time complexity 0(n2)
13+
//Better approach without convert integer in string and in array, just working with integer number finding the half integer and compare it with original
14+
//0(logn100)

0 commit comments

Comments
 (0)