File tree Expand file tree Collapse file tree 3 files changed +77
-24
lines changed Expand file tree Collapse file tree 3 files changed +77
-24
lines changed Original file line number Diff line number Diff line change 1
1
# [ 1. Add Sum] [ title ]
2
2
3
3
## Description
4
-
5
- Given two binary strings, return their sum (also a binary string).
6
-
7
- The input strings are both ** non-empty** and contains only characters ` 1 ` or ` 0 ` .
8
-
4
+ 一个只包含 A-Z 的消息可以用如下方式编码成数字:
5
+ ``` bash
6
+ ' A' -> 1
7
+ ' B' -> 2
8
+ ...
9
+ ' Z' -> 26
10
+ ```
11
+ 给定一个只包含数字的非空字符串,返回共有多少种解码方案。
9
12
** Example 1:**
10
13
11
14
```
12
- Input: a = "11", b = "1"
13
- Output: "100"
15
+ Input: "12"
16
+ Output: 2
17
+ Explanation: It could be decoded as "AB" (1 2) or "L" (12).
14
18
```
15
19
16
20
** Example 2:**
17
21
18
22
```
19
- Input: a = "1010", b = "1011"
20
- Output: "10101"
23
+ Input: "226"
24
+ Output: 3
25
+ Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
21
26
```
22
27
23
28
** Tags:** Math, String
@@ -28,16 +33,40 @@ Output: "10101"
28
33
## 题解
29
34
30
35
### 思路1
31
- > 按照小学算数那么来做,用 ` carry ` 表示进位,从后往前算,依次往前,每算出一位就插入到最前面即
8000
可,直到把两个二进制串都遍历完即可。
36
+ > (动态规划) O(n)
37
+ >
38
+ 这道题目可以用动态规划来做。
39
+ 状态表示:f[ i] f[ i] 表示前 ii 个数字共有多少种解码方式。
40
+ 初始化:0个数字解码的方案数1,即 f[ 0] =1f[ 0] =1。
41
+ 状态转移:f[ i] f[ i] 可以表示成如下两部分的和:
32
42
33
- ``` go
34
-
35
- ```
43
+ - 如果第 ii 个数字不是0,则 ii 个数字可以单独解码成一个字母,此时的方案数等于用前 i−1i−1 个数字解码的方案数,即 f [ i−1 ] f [ i−1 ] ;
44
+ - 如果第 i−1i−1个数字和第 ii 个数字组成的两位数在 1010 到 2626 之间,则可以将这两位数字解码成一个字符,此时的方案数等于用前 i−2i−2 个数字解码的方案数,即 f [ i−2 ] f [ i−2 ] ;
45
+ 时间复杂度分析:状态数是 nn 个,状态转移的时间复杂度是 O(1)O(1),所以总时间复杂度是 O(n)O(n)。
36
46
37
- ### 思路2
38
- > 思路2
39
47
``` go
40
-
48
+ func numDecodings (s string ) int {
49
+ n := len (s)
50
+ f := make ([]int , n+1 )
51
+ f[0 ] = 1
52
+
53
+ for i := 1 ; i <= n; i++ {
54
+ if s[i-1 ] < ' 0' || s[i-1 ] > ' 9' {
55
+ return 0
56
+ }
57
+ f[i] = 0
58
+ if s[i-1 ] != ' 0' {
59
+ f[i] = f[i-1 ]
60
+ }
61
+ if i > 1 {
62
+ t := (s[i-2 ]-' 0' )*10 + s[i-1 ] - ' 0'
63
+ if t >= 10 && t <= 26 {
64
+ f[i] += f[i-2 ]
65
+ }
66
+ }
67
+ }
68
+ return f[n]
69
+ }
41
70
```
42
71
43
72
## 结语
Original file line number Diff line number Diff line change 1
1
package Solution
2
2
3
- func Solution (x bool ) bool {
4
- return x
3
+ func numDecodings (s string ) int {
4
+ // 思路:
5
+ // 状态表示:f[i] i个数(最后一个数字是s[i-1])解码得到的所有字符串的数量 f[i]从0开始
6
+ // 状态计算: 集合可以分成:最后一个i是一位数,和最后一个i是两位数
7
+ // 之前想过的错误思路:f[i] 表示以i结尾的decode共有多少种解法
8
+ // 集合划分为不算i:f[i - j] + f[j] 和算i: f[i];
9
+ n := len (s )
10
+ //加一是为了方便边界特判
11
+ f := make ([]int , n + 1 )
12
+ //空字符串特判
13
+ f [0 ] = 1
14
+ // 如果最后一个i是个位数 1 ~ 26
15
+ // 难点:如何转化char为int
16
+ for i := 1 ; i <= n ; i ++ { //从一开始是因为表示的是前多少个字母,不是下标从一开始的字母
17
+ //如果最后一个i是个位数,且不为0
18
+ if s [i - 1 ] != '0' {
19
+ f [i ] += f [i - 1 ]
20
+ }
21
+ //如果最后一个i是两位数,且在1~26之间
22
+ if i >= 2 {
23
+ t := (s [i - 2 ]- '0' )* 10 + s [i - 1 ] - '0'
24
+ if t >= 10 && t <= 26 {
25
+ f [i ] += f [i - 2 ]
26
+ }
27
+ }
28
+ }
29
+ return f [n ]
5
30
}
Original file line number Diff line number Diff line change @@ -9,18 +9,17 @@ func TestSolution(t *testing.T) {
9
9
// 测试用例
10
10
cases := []struct {
11
11
name string
12
- inputs bool
13
- expect bool
12
+ inputs string
13
+ expect int
14
14
}{
15
- {"TestCacse 1" , true , true },
16
- {"TestCacse 1" , true , true },
17
- {"TestCacse 1" , false , false },
15
+ {"TestCacse 1" , "12" , 2 },
16
+ {"TestCacse 1" , "226" , 3 },
18
17
}
19
18
20
19
// 开始测试
21
20
for _ , c := range cases {
22
21
t .Run (c .name , func (t * testing.T ) {
23
- ret := Solution (c .inputs )
22
+ ret := numDecodings (c .inputs )
24
23
if ! reflect .DeepEqual (ret , c .expect ) {
25
24
t .Fatalf ("expected: %v, but got: %v, with inputs: %v" ,
26
25
c .expect , ret , c .inputs )
Yo
10FF
u can’t perform that action at this time.
0 commit comments