-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpalindrome-number.js
More file actions
62 lines (53 loc) · 1.73 KB
/
palindrome-number.js
File metadata and controls
62 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// https://leetcode.com/problems/palindrome-number/
// Related Topics: Math, Array
// Difficulty: Easy
/*
Initial thoughts:
Converting the number to an array of strings
we can reverse the array and check wheter it
equals the original.
Time complexity: O(log n) because x has approximately log10(n) digits
Space complexity: O(log n) because we need to convert the number to string
*/
/**
* @param {number} x
* @return {boolean}
*/
const isPalindrome = x => {
let revX = x.toString().split('');
// It's enough to copy half of the array
// bc we are checking for a palindrome
for (let i = 0; i < revX.length / 2; i++) revX[revX.length - i - 1] = revX[i];
return x.toString() === revX.join('');
};
/*
Optimization:
Using basic math and arithmetic, we can reverse the number.
Problem is that we could run into an overflow.
The idea is to only revert half of the number and compare it
to the other half, using the property of palindromes that its
first half must equal the second. If it has an odd number of digits
that ditit (the middle one) does not affect our algorithm.
Time complexity: O(log n) because x has approximately log10(n) digits
Space complexity: O(1)
*/
/**
* @param {number} x
* @return {boolean}
*/
const isPalindrome = x => {
// handling edge cases
if (x < 0 || (x % 10 === 0 && x != 0)) {
return false;
}
let revertedNumber = 0;
// when x becomes equal or less than the reverted number
// we know that we have reverted the right half of the number
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + (x % 10);
x = (x / 10) | 0;
}
// making sure that if it has an odd number of digits
// we still catch the palindrome
return x === revertedNumber || x === ((revertedNumber / 10) | 0);
};