File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -10,5 +10,6 @@ leetcode for golang
1010#### [ 6. zigzag conversion] ( https://github.com/hitzzc/go-leetcode/tree/master/zigzag_conversion )
1111#### [ 7. reverse integer] ( https://github.com/hitzzc/go-leetcode/tree/master/reverse_integer )
1212#### [ 8. string to integer] ( https://github.com/hitzzc/go-leetcode/tree/master/string_to_integer )
13+ #### [ 9. palindrome number] ( https://github.com/hitzzc/go-leetcode/tree/master/palindrome_number )
1314
1415
Original file line number Diff line number Diff line change 1+ package palindrome_number
2+
3+ func isPalindrome (x int ) bool {
4+ if x < 0 {
5+ return false
6+ }
7+ bits := 1
8+ tmp := x
9+ for {
10+ if tmp < 10 {
11+ break
12+ }
13+ tmp = tmp / 10
14+ bits ++
15+ }
16+ i , j := 1 , bits
17+ for i < j {
18+ left := (x / divisor (i )) % 10
19+ right := (x / divisor (j )) % 10
20+ if left != right {
21+ return false
22+ }
23+ i , j = i + 1 , j - 1
24+ }
25+ return true
26+ }
27+
28+ func divisor (bits int ) int {
29+ ret := 1
30+ for i := 0 ; i < bits - 1 ; i ++ {
31+ ret = ret * 10
32+ }
33+ return ret
34+ }
Original file line number Diff line number Diff line change 1+ package palindrome_number
2+
3+ import (
4+ "testing"
5+ )
6+
7+ func TestIsPalindrome (t * testing.T ) {
8+
9+ }
You can’t perform that action at this time.
0 commit comments