Skip to content

Commit a232734

Browse files
authored
Create palindrome-algorithm.ts
1 parent db40bba commit a232734

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

palindrome-algorithm.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Given an integer x, return true if x is a
2+
// palindrome
3+
// , and false otherwise.
4+
5+
// Example 1:
6+
7+
// Input: x = 121
8+
// Output: true
9+
// Explanation: 121 reads as 121 from left to right and from right to left.
10+
// Example 2:
11+
12+
// Input: x = -121
13+
// Output: false
14+
// Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
15+
// Example 3:
16+
17+
// Input: x = 10
18+
// Output: false
19+
// Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
20+
21+
// Constraints:
22+
23+
// -231 <= x <= 231 - 1
24+
25+
// Follow up: Could you solve it without converting the integer to a string?
26+
27+
//More examples for me
28+
//madam
29+
//121
30+
//101
31+
//aba
32+
//202
33+
//..
34+
35+
function isPalindrome(x: number): boolean {
36+
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
37+
38+
let reversedValue = x.toString().split("").reverse().join("");
39+
40+
return x === parseInt(reversedValue) ? true : false;
41+
}
42+
43+
console.log(isPalindrome(120));

0 commit comments

Comments
 (0)