Skip to content

Commit 7027ec7

Browse files
committed
feat: add solutions to lc problem: No.0091. Decode Ways
1 parent 67bae26 commit 7027ec7

File tree

6 files changed

+189
-2
lines changed

6 files changed

+189
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@
180180
- [单词拆分](./solution/0100-0199/0139.Word%20Break/README.md)
181181
- [接雨水](./solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
182182
- [等差数列划分](./solution/0400-0499/0413.Arithmetic%20Slices/README.md)
183+
- [解码方法](./solution/0000-0099/0091.Decode%20Ways/README.md)
183184
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
184185
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)
185-
- [解码方法](./solution/0000-0099/0091.Decode%20Ways/README.md)
186186
- [最长上升子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)
187187
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
188188
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
174174
- [Word Break](./solution/0100-0199/0139.Word%20Break/README_EN.md)
175175
- [Trapping Rain Water](./solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
176176
- [Arithmetic Slices](./solution/0400-0499/0413.Arithmetic%20Slices/README_EN.md)
177-
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
178177
- [Decode Ways](./solution/0000-0099/0091.Decode%20Ways/README_EN.md)
178+
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
179179
- [Longest Increasing Subsequence](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
180180
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
181181
- [Longest Common Subsequence](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)

solution/0000-0099/0091.Decode Ways/README.md

+75
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,81 @@ class Solution {
173173
}
174174
```
175175

176+
### **C++**
177+
178+
```cpp
179+
class Solution {
180+
public:
181+
int numDecodings(string s) {
182+
int n = s.size();
183+
vector<int> dp(n + 1);
184+
dp[0] = 1;
185+
for (int i = 1; i <= n; ++i) {
186+
if (s[i - 1] != '0') {
187+
dp[i] += dp[i - 1];
188+
}
189+
if (i > 1 && s[i - 2] != '0') {
190+
if ((s[i - 2] - '0') * 10 + s[i - 1] - '0' <= 26) {
191+
dp[i] += dp[i - 2];
192+
}
193+
}
194+
}
195+
return dp[n];
196+
}
197+
};
198+
```
199+
200+
### **Go**
201+
202+
```go
203+
func numDecodings(s string) int {
204+
n := len(s)
205+
dp := make([]int, n+1)
206+
dp[0] = 1
207+
for i := 1; i <= n; i++ {
208+
if s[i-1] != '0' {
209+
dp[i] += dp[i-1]
210+
}
211+
if i > 1 && s[i-2] != '0' {
212+
if (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
213+
dp[i] += dp[i-2]
214+
}
215+
}
216+
}
217+
return dp[n]
218+
}
219+
```
220+
221+
### **C#**
222+
223+
```cs
224+
public class Solution {
225+
public int NumDecodings(string s) {
226+
if (s.Length == 0) return 0;
227+
228+
var f0 = 1;
229+
var f1 = 1;
230+
var f2 = 1;
231+
for (var i = 0; i < s.Length; ++i)
232+
{
233+
f0 = f1;
234+
f1 = f2;
235+
f2 = 0;
236+
var two = i > 0 ? int.Parse(string.Format("{0}{1}", s[i - 1], s[i])) : 0;
237+
if (two >= 10 && two <= 26)
238+
{
239+
f2 += f0;
240+
}
241+
if (s[i] != '0')
242+
{
243+
f2 += f1;
244+
}
245+
}
246+
return f2;
247+
}
248+
}
249+
```
250+
176251
### **...**
177252

178253
```

solution/0000-0099/0091.Decode Ways/README_EN.md

+77
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Hence, there are no valid ways to decode this since all digits need to be mapped
7272

7373
## Solutions
7474

75+
Dynamic programming.
76+
7577
<!-- tabs:start -->
7678

7779
### **Python3**
@@ -155,6 +157,81 @@ class Solution {
155157
}
156158
```
157159

160+
### **C++**
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
int numDecodings(string s) {
166+
int n = s.size();
167+
vector<int> dp(n + 1);
168+
dp[0] = 1;
169+
for (int i = 1; i <= n; ++i) {
170+
if (s[i - 1] != '0') {
171+
dp[i] += dp[i - 1];
172+
}
173+
if (i > 1 && s[i - 2] != '0') {
174+
if ((s[i - 2] - '0') * 10 + s[i - 1] - '0' <= 26) {
175+
dp[i] += dp[i - 2];
176+
}
177+
}
178+
}
179+
return dp[n];
180+
}
181+
};
182+
```
183+
184+
### **Go**
185+
186+
```go
187+
func numDecodings(s string) int {
188+
n := len(s)
189+
dp := make([]int, n+1)
190+
dp[0] = 1
191+
for i := 1; i <= n; i++ {
192+
if s[i-1] != '0' {
193+
dp[i] += dp[i-1]
194+
}
195+
if i > 1 && s[i-2] != '0' {
196+
if (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
197+
dp[i] += dp[i-2]
198+
}
199+
}
200+
}
201+
return dp[n]
202+
}
203+
```
204+
205+
### **C#**
206+
207+
```cs
208+
public class Solution {
209+
public int NumDecodings(string s) {
210+
if (s.Length == 0) return 0;
211+
212+
var f0 = 1;
213+
var f1 = 1;
214+
var f2 = 1;
215+
for (var i = 0; i < s.Length; ++i)
216+
{
217+
f0 = f1;
218+
f1 = f2;
219+
f2 = 0;
220+
var two = i > 0 ? int.Parse(string.Format("{0}{1}", s[i - 1], s[i])) : 0;
221+
if (two >= 10 && two <= 26)
222+
{
223+
f2 += f0;
224+
}
225+
if (s[i] != '0')
226+
{
227+
f2 += f1;
228+
}
229+
}
230+
return f2;
231+
}
232+
}
233+
```
234+
158235
### **...**
159236

160237
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
int n = s.size();
5+
vector<int> dp(n + 1);
6+
dp[0] = 1;
7+
for (int i = 1; i <= n; ++i) {
8+
if (s[i - 1] != '0') {
9+
dp[i] += dp[i - 1];
10+
}
11+
if (i > 1 && s[i - 2] != '0') {
12+
if ((s[i - 2] - '0') * 10 + s[i - 1] - '0' <= 26) {
13+
dp[i] += dp[i - 2];
14+
}
15+
}
16+
}
17+
return dp[n];
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func numDecodings(s string) int {
2+
n := len(s)
3+
dp := make([]int, n+1)
4+
dp[0] = 1
5+
for i := 1; i <= n; i++ {
6+
if s[i-1] != '0' {
7+
dp[i] += dp[i-1]
8+
}
9+
if i > 1 && s[i-2] != '0' {
10+
if (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
11+
dp[i] += dp[i-2]
12+
}
13+
}
14+
}
15+
return dp[n]
16+
}

0 commit comments

Comments
 (0)