Skip to content

Commit 93736f5

Browse files
author
yangguang_chen
committed
* check palindrome number
1 parent e0c79b4 commit 93736f5

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## go solution
44
- [1.two-sum](./src/1-TwoSum/1-two-sum.go)
55
- [7.reverse-integer](./src/7-ReverseInteger/7-reverse-integer.go)
6+
- [9.palindrome-number](./src/9-PalindromeNumbe/palindrome_num.go)
67
- [14.longest-common-prefix](./src/14-LongestCommonPrefix/14_longest_common_prefix.go)
78
- [27.remove-element](./src/27-RemoveElement/rm_ele.go)
89
- [35.search-insert-position](./src/35-SearchInsertPosition/search_insert_position.go)

src/70-ClimbingStairs/climb_stairs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cs
2+
3+
// ClimbStairs calculate n steps to reach the top
4+
// You are climbing a stair case. It takes n steps to reach to the top.
5+
// Each time you can either climb 1 or 2 steps.
6+
// In how many distinct ways can you climb to the top?
7+
// Note: Given n will be a positive integer.
8+
// see more from https://leetcode.com/problems/climbing-stairs/description/
9+
func ClimbStairs(n int) int {
10+
11+
return count
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cs
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestClimbStairs(t *testing.T) {
8+
var n = 6
9+
nums := ClimbStairs(n)
10+
if nums != 8 {
11+
t.Error("fail")
12+
}
13+
t.Log(nums)
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package pn
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
// PalindromeNumber Determine whether an integer is a palindrome.
9+
// An integer is a palindrome when it reads the same backward as forward.
10+
11+
func PalindromeNumber(x int) bool {
12+
if x < 0 {
13+
return false
14+
}
15+
arr := strings.Split(strconv.Itoa(x), "")
16+
lens := len(arr)
17+
for k, v := range arr {
18+
if k <= (lens/2) && v != arr[lens-1-k] {
19+
return false
20+
}
21+
}
22+
return true
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pn
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPalindromeNumber(t *testing.T) {
8+
var num = 343
9+
if ok := PalindromeNumber(num); !ok {
10+
t.Error(num, " is not a palindrome number. ")
11+
}
12+
t.Log(num)
13+
14+
num = 12345321
15+
if ok := PalindromeNumber(num); !ok {
16+
t.Error(num, " is not a palindrome number. ")
17+
}
18+
t.Log(num)
19+
20+
num = -121
21+
if ok := PalindromeNumber(num); !ok {
22+
t.Error(num, " is not a palindrome number. ")
23+
}
24+
t.Log(num)
25+
}

0 commit comments

Comments
 (0)