Skip to content

Commit c3e94b7

Browse files
committed
add two solution
1 parent 816e161 commit c3e94b7

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed

leetcode/decode-ways.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
// https://leetcode.com/problems/decode-ways/
4+
5+
func isValidEncode(s string) bool {
6+
if len(s) == 1 {
7+
return s >= "1" && s <= "9"
8+
}
9+
if s <= "26" && s >= "10" {
10+
return true
11+
}
12+
return false
13+
}
14+
15+
func numDecodings(s string) int {
16+
if len(s) == 0 {
17+
return 0
18+
}
19+
dp := make([]int, len(s))
20+
if isValidEncode(s[0:1]) {
21+
dp[0] = 1
22+
}
23+
if len(s) == 1 {
24+
return dp[0]
25+
}
26+
for i := 1; i < len(s); i++ {
27+
total := 0
28+
if isValidEncode(s[i : i+1]) {
29+
total = dp[i-1]
30+
}
31+
if isValidEncode(s[i-1 : i+1]) {
32+
if i == 1 {
33+
total++
34+
} else {
35+
total = total + dp[i-2]
36+
}
37+
}
38+
dp[i] = total
39+
}
40+
41+
return dp[len(s)-1]
42+
}

leetcode/decode-ways_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func TestNumDecodings(t *testing.T) {
6+
tests := []struct {
7+
input string
8+
expect int
9+
}{
10+
{
11+
input: "12",
12+
expect: 2,
13+
}, {
14+
input: "22",
15+
expect: 2,
16+
}, {
17+
input: "226",
18+
expect: 3,
19+
}, {
20+
input: "0",
21+
expect: 0,
22+
}, {
23+
input: "010",
24+
expect: 0,
25+
}, {
26+
input: "0010",
27+
expect: 0,
28+
}, {
29+
input: "10",
30+
expect: 1,
31+
}, {
32+
input: "100",
33+
expect: 0,
34+
},
35+
}
36+
for i, item := range tests {
37+
actual := numDecodings(item.input)
38+
if actual != item.expect {
39+
t.Fatalf("%d, expect %d, actual %d\n", i, item.expect, actual)
40+
}
41+
}
42+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package leetcode
2+
3+
// https://leetcode.com/problems/longest-palindromic-substring/
4+
5+
func findPalindrome(s string, i int) int {
6+
if s[i] == s[i-1] {
7+
return i - 1
8+
}
9+
if i-2 >= 0 && s[i] == s[i-2] {
10+
return i - 2
11+
}
12+
return -1
13+
}
14+
15+
func isPalindrome(s string) bool {
16+
if len(s) < 2 {
17+
return false
18+
}
19+
i := 0
20+
j := len(s) - 1
21+
for i < j {
22+
if s[i] != s[j] {
23+
return false
24+
}
25+
i++
26+
j--
27+
}
28+
return true
29+
}
30+
31+
func longestPalindrome(s string) string {
32+
if len(s) < 1 {
33+
return ""
34+
}
35+
maxLen := 0
36+
maxPos := 0
37+
lens := make([]int, len(s))
38+
lens[0] = -1
39+
for i := 1; i < len(s); i++ {
40+
lens[i] = -1
41+
front := lens[i-1]
42+
if front == -1 {
43+
lens[i] = findPalindrome(s, i)
44+
// fmt.Printf("0, %d %d\n", i, lens[i])
45+
} else {
46+
if front > 0 && s[i] == s[front-1] {
47+
lens[i] = front - 1
48+
for {
49+
if lens[i] > 0 && lens[lens[i]] >= 0 {
50+
if isPalindrome(s[lens[lens[i]] : i+1]) {
51+
lens[i] = lens[lens[i]]
52+
} else {
53+
break
54+
}
55+
} else {
56+
break
57+
}
58+
}
59+
} else {
60+
s2 := s[front : i+1]
61+
// fmt.Printf("2, %d %d[%s]", front, i, s2)
62+
if isPalindrome(s2) {
63+
lens[i] = front
64+
}
65+
}
66+
}
67+
if lens[i] == -1 {
68+
lens[i] = findPalindrome(s, i)
69+
}
70+
newLen := i - lens[i] + 1
71+
if lens[i] > -1 && newLen > maxLen {
72+
maxLen = newLen
73+
maxPos = i
74+
}
75+
// fmt.Printf("i: %d, lens[i]:%d, maxLen: %d\n", i, lens[i], maxLen)
76+
}
77+
78+
if maxLen == 0 {
79+
return s[0:1]
80+
}
81+
// fmt.Printf("ret %d %d %v\n", maxPos, lens[maxPos], lens)
82+
return s[lens[maxPos] : maxPos+1]
83+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func TestLongestPalindrome(t *testing.T) {
6+
tests := []struct {
7+
input string
8+
expect string
9+
}{
10+
{
11+
input: "bananas",
12+
expect: "anana",
13+
}, {
14+
input: "babad",
15+
expect: "bab",
16+
}, {
17+
input: "ccc",
18+
expect: "ccc",
19+
}, {
20+
input: "cbbd",
21+
expect: "bb",
22+
}, {
23+
input: "abcdefg",
24+
expect: "a",
25+
}, {
26+
input: "bbbbbb",
27+
expect: "bbbbbb",
28+
}, {
29+
input: "abababac",
30+
expect: "abababa",
31+
}, {
32+
input: "abababab",
33+
expect: "abababa",
34+
}, {
35+
input: "aaaabbbbbb",
36+
expect: "bbbbbb",
37+
}, {
38+
input: "abcdcba",
39+
expect: "abcdcba",
40+
}, {
41+
input: "",
42+
expect: "",
43+
}, {
44+
input: "a",
45+
expect: "a",
46+
},
47+
}
48+
49+
for i, item := range tests {
50+
actual := longestPalindrome2(item.input)
51+
if actual != item.expect {
52+
t.Fatalf("%d expect %s, actual %s", i, item.expect, actual)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)