Skip to content

Commit 12ccea1

Browse files
committed
feat: add solutions to lc problem: No.0639.Decode Ways II
1 parent 1dd9ff7 commit 12ccea1

File tree

5 files changed

+399
-2
lines changed

5 files changed

+399
-2
lines changed

solution/0600-0699/0639.Decode Ways II/README.md

+135-1
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,156 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
只是在 [91. 解码方法](/solution/0000-0099/0091.Decode%20Ways/README.md) 的基础上加了些关于 `*` 的条件判断
49+
4850
<!-- tabs:start -->
4951

5052
### **Python3**
5153

5254
<!-- 这里可写当前语言的特殊实现逻辑 -->
5355

5456
```python
55-
57+
class Solution:
58+
def numDecodings(self, s: str) -> int:
59+
mod = int(1e9 + 7)
60+
n = len(s)
61+
62+
# dp[i - 2], dp[i - 1], dp[i]
63+
a, b, c = 0, 1, 0
64+
for i in range(1, n + 1):
65+
# 1 digit
66+
if s[i - 1] == "*":
67+
c = 9 * b % mod
68+
elif s[i - 1] != "0":
69+
c = b
70+
else:
71+
c = 0
72+
73+
# 2 digits
74+
if i > 1:
75+
if s[i - 2] == "*" and s[i - 1] == "*":
76+
c = (c + 15 * a) % mod
77+
elif s[i - 2] == "*":
78+
if s[i - 1] > "6":
79+
c = (c + a) % mod
80+
else:
81+
c = (c + 2 * a) % mod
82+
elif s[i - 1] == "*":
83+
if s[i - 2] == "1":
84+
c = (c + 9 * a) % mod
85+
elif s[i - 2] == "2":
86+
c = (c + 6 * a) % mod
87+
elif (
88+
s[i - 2] != "0"
89+
and (ord(s[i - 2]) - ord("0")) * 10 + ord(s[i - 1]) - ord("0") <= 26
90+
):
91+
c = (c + a) % mod
92+
93+
a, b = b, c
94+
95+
return c
5696
```
5797

5898
### **Java**
5999

60100
<!-- 这里可写当前语言的特殊实现逻辑 -->
61101

62102
```java
103+
class Solution {
104+
105+
private static final int MOD = 1000000007;
106+
107+
public int numDecodings(String s) {
108+
int n = s.length();
109+
char[] cs = s.toCharArray();
110+
111+
// dp[i - 2], dp[i - 1], dp[i]
112+
long a = 0, b = 1, c = 0;
113+
for (int i = 1; i <= n; i++) {
114+
// 1 digit
115+
if (cs[i - 1] == '*') {
116+
c = 9 * b % MOD;
117+
} else if (cs[i - 1] != '0') {
118+
c = b;
119+
} else {
120+
c = 0;
121+
}
122+
123+
// 2 digits
124+
if (i > 1) {
125+
if (cs[i - 2] == '*' && cs[i - 1] == '*') {
126+
c = (c + 15 * a) % MOD;
127+
} else if (cs[i - 2] == '*') {
128+
if (cs[i - 1] > '6') {
129+
c = (c + a) % MOD;
130+
} else {
131+
c = (c + 2 * a) % MOD;
132+
}
133+
} else if (cs[i - 1] == '*') {
134+
if (cs[i - 2] == '1') {
135+
c = (c + 9 * a) % MOD;
136+
} else if (cs[i - 2] == '2') {
137+
c = (c + 6 * a) % MOD;
138+
}
139+
} else if (cs[i - 2] != '0' && (cs[i - 2] - '0') * 10 + cs[i - 1] - '0' <= 26) {
140+
c = (c + a) % MOD;
141+
}
142+
}
143+
144+
a = b;
145+
b = c;
146+
}
147+
148+
return (int) c;
149+
}
150+
}
151+
```
63152

153+
### **Go**
154+
155+
```go
156+
const mod int = 1e9 + 7
157+
158+
func numDecodings(s string) int {
159+
n := len(s)
160+
161+
// dp[i - 2], dp[i - 1], dp[i]
162+
a, b, c := 0, 1, 0
163+
for i := 1; i <= n; i++ {
164+
// 1 digit
165+
if s[i-1] == '*' {
166+
c = 9 * b % mod
167+
} else if s[i-1] != '0' {
168+
c = b
169+
} else {
170+
c = 0
171+
}
172+
173+
// 2 digits
174+
if i > 1 {
175+
if s[i-2] == '*' && s[i-1] == '*' {
176+
c = (c + 15*a) % mod
177+
} else if s[i-2] == '*' {
178+
if s[i-1] > '6' {
179+
c = (c + a) % mod
180+
} else {
181+
c = (c + 2*a) % mod
182+
}
183+
} else if s[i-1] == '*' {
184+
if s[i-2] == '1' {
185+
c = (c + 9*a) % mod
186+
} else if s[i-2] == '2' {
187+
c = (c + 6*a) % mod
188+
}
189+
} else if s[i-2] != '0' && (s[i-2]-'0')*10+s[i-1]-'0' <= 26 {
190+
c = (c + a) % mod
191+
}
192+
}
193+
194+
a, b = b, c
195+
}
196+
return c
197+
}
64198
```
65199

66200
### **...**

solution/0600-0699/0639.Decode Ways II/README_EN.md

+135-1
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,152 @@ Hence, there are a total of (6 * 2) + (3 * 1) = 12 + 3 = 15 ways to decode &quot
7070

7171
## Solutions
7272

73+
It's just that some conditional judgments about `*` have been added to the [91. Decode Ways](/solution/0000-0099/0091.Decode%20Ways/README_EN.md).
74+
7375
<!-- tabs:start -->
7476

7577
### **Python3**
7678

7779
```python
78-
80+
class Solution:
81+
def numDecodings(self, s: str) -> int:
82+
mod = int(1e9 + 7)
83+
n = len(s)
84+
85+
# dp[i - 2], dp[i - 1], dp[i]
86+
a, b, c = 0, 1, 0
87+
for i in range(1, n + 1):
88+
# 1 digit
89+
if s[i - 1] == "*":
90+
c = 9 * b % mod
91+
elif s[i - 1] != "0":
92+
c = b
93+
else:
94+
c = 0
95+
96+
# 2 digits
97+
if i > 1:
98+
if s[i - 2] == "*" and s[i - 1] == "*":
99+
c = (c + 15 * a) % mod
100+
elif s[i - 2] == "*":
101+
if s[i - 1] > "6":
102+
c = (c + a) % mod
103+
else:
104+
c = (c + 2 * a) % mod
105+
elif s[i - 1] == "*":
106+
if s[i - 2] == "1":
107+
c = (c + 9 * a) % mod
108+
elif s[i - 2] == "2":
109+
c = (c + 6 * a) % mod
110+
elif (
111+
s[i - 2] != "0"
112+
and (ord(s[i - 2]) - ord("0")) * 10 + ord(s[i - 1]) - ord("0") <= 26
113+
):
114+
c = (c + a) % mod
115+
116+
a, b = b, c
117+
118+
return c
79119
```
80120

81121
### **Java**
82122

83123
```java
124+
class Solution {
125+
126+
private static final int MOD = 1000000007;
127+
128+
public int numDecodings(String s) {
129+
int n = s.length();
130+
char[] cs = s.toCharArray();
131+
132+
// dp[i - 2], dp[i - 1], dp[i]
133+
long a = 0, b = 1, c = 0;
134+
for (int i = 1; i <= n; i++) {
135+
// 1 digit
136+
if (cs[i - 1] == '*') {
137+
c = 9 * b % MOD;
138+
} else if (cs[i - 1] != '0') {
139+
c = b;
140+
} else {
141+
c = 0;
142+
}
143+
144+
// 2 digits
145+
if (i > 1) {
146+
if (cs[i - 2] == '*' && cs[i - 1] == '*') {
147+
c = (c + 15 * a) % MOD;
148+
} else if (cs[i - 2] == '*') {
149+
if (cs[i - 1] > '6') {
150+
c = (c + a) % MOD;
151+
} else {
152+
c = (c + 2 * a) % MOD;
153+
}
154+
} else if (cs[i - 1] == '*') {
155+
if (cs[i - 2] == '1') {
156+
c = (c + 9 * a) % MOD;
157+
} else if (cs[i - 2] == '2') {
158+
c = (c + 6 * a) % MOD;
159+
}
160+
} else if (cs[i - 2] != '0' && (cs[i - 2] - '0') * 10 + cs[i - 1] - '0' <= 26) {
161+
c = (c + a) % MOD;
162+
}
163+
}
164+
165+
a = b;
166+
b = c;
167+
}
168+
169+
return (int) c;
170+
}
171+
}
172+
```
84173

174+
### **Go**
175+
176+
```go
177+
const mod int = 1e9 + 7
178+
179+
func numDecodings(s string) int {
180+
n := len(s)
181+
182+
// dp[i - 2], dp[i - 1], dp[i]
183+
a, b, c := 0, 1, 0
184+
for i := 1; i <= n; i++ {
185+
// 1 digit
186+
if s[i-1] == '*' {
187+
c = 9 * b % mod
188+
} else if s[i-1] != '0' {
189+
c = b
190+
} else {
191+
c = 0
192+
}
193+
194+
// 2 digits
195+
if i > 1 {
196+
if s[i-2] == '*' && s[i-1] == '*' {
197+
c = (c + 15*a) % mod
198+
} else if s[i-2] == '*' {
199+
if s[i-1] > '6' {
200+
c = (c + a) % mod
201+
} else {
202+
c = (c + 2*a) % mod
203+
}
204+
} else if s[i-1] == '*' {
205+
if s[i-2] == '1' {
206+
c = (c + 9*a) % mod
207+
} else if s[i-2] == '2' {
208+
c = (c + 6*a) % mod
209+
}
210+
} else if s[i-2] != '0' && (s[i-2]-'0')*10+s[i-1]-'0' <= 26 {
211+
c = (c + a) % mod
212+
}
213+
}
214+
215+
a, b = b, c
216+
}
217+
return c
218+
}
85219
```
86220

87221
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const mod int = 1e9 + 7
2+
3+
func numDecodings(s string) int {
4+
n := len(s)
5+
6+
// dp[i - 2], dp[i - 1], dp[i]
7+
a, b, c := 0, 1, 0
8+
for i := 1; i <= n; i++ {
9+
// 1 digit
10+
if s[i-1] == '*' {
11+
c = 9 * b % mod
12+
} else if s[i-1] != '0' {
13+
c = b
14+
} else {
15+
c = 0
16+
}
17+
18+
// 2 digits
19+
if i > 1 {
20+
if s[i-2] == '*' && s[i-1] == '*' {
21+
c = (c + 15*a) % mod
22+
} else if s[i-2] == '*' {
23+
if s[i-1] > '6' {
24+
c = (c + a) % mod
25+
} else {
26+
c = (c + 2*a) % mod
27+
}
28+
} else if s[i-1] == '*' {
29+
if s[i-2] == '1' {
30+
c = (c + 9*a) % mod
31+
} else if s[i-2] == '2' {
32+
c = (c + 6*a) % mod
33+
}
34+
} else if s[i-2] != '0' && (s[i-2]-'0')*10+s[i-1]-'0' <= 26 {
35+
c = (c + a) % mod
36+
}
37+
}
38+
39+
a, b = b, c
40+
}
41+
return c
42+
}

0 commit comments

Comments
 (0)