Skip to content

Commit 98ffaa0

Browse files
committed
palindrome number
1 parent 8175c97 commit 98ffaa0

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

9.palindrome-number.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode id=9 lang=golang
5+
*
6+
* [9] Palindrome Number
7+
*/
8+
9+
// @lc code=start
10+
func isPalindrome(x int) bool {
11+
if x < 0 {
12+
return false
13+
}
14+
if x < 10 {
15+
return true
16+
}
17+
if x%10 == 0 {
18+
return false
19+
}
20+
var y int
21+
for x > y {
22+
y = y*10 + x%10
23+
x = x / 10
24+
println(x, y)
25+
}
26+
return x == y || x == y/10
27+
}
28+
29+
// @lc code=end

9.palindrome-number.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=9 lang=javascript
3+
*
4+
* [9] Palindrome Number
5+
*/
6+
7+
// @lc code=start
8+
function isPalindrome(x) {
9+
if (x < 0) {
10+
return false;
11+
}
12+
13+
let num = x;
14+
let reverse = 0;
15+
16+
while (num > 0) {
17+
reverse = reverse * 10 + (num % 10);
18+
num = Math.floor(num / 10);
19+
}
20+
21+
return reverse === x;
22+
}
23+
24+
// @lc code=end

0 commit comments

Comments
 (0)