Skip to content

Commit

Permalink
feat: update leetcode solutions: No.0091. Decode Ways
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 21, 2021
1 parent 9945e3c commit 90b3883
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
- [接雨水](/solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
- [最大子序和](/solution/0000-0099/0053.Maximum%20Subarray/README.md)
- [礼物的最大价值](/lcof/面试题47.%20礼物的最大价值/README.md)
- [解码方法](/solution/0000-0099/0091.Decode%20Ways/README.md)
- [乘积最大子序列](/solution/0100-0199/0152.Maximum%20Product%20Subarray/README.md)
- [打家劫舍](/solution/0100-0199/0198.House%20Robber/README.md)
- [打家劫舍 II](/solution/0200-0299/0213.House%20Robber%20II/README.md)
Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
- [Climbing Stairs](/solution/0000-0099/0070.Climbing%20Stairs/README_EN.md)
- [Trapping Rain Water](/solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
- [Maximum Subarray](/solution/0000-0099/0053.Maximum%20Subarray/README_EN.md)
- [Decode Ways](/solution/0000-0099/0091.Decode%20Ways/README_EN.md)
- [Maximum Product Subarray](/solution/0100-0199/0152.Maximum%20Product%20Subarray/README_EN.md)
- [House Robber](/solution/0100-0199/0198.House%20Robber/README_EN.md)
- [House Robber II](/solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
Expand Down
78 changes: 78 additions & 0 deletions solution/0000-0099/0091.Decode Ways/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,100 @@

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

动态规划法。

假设 `dp[i]` 表示字符串 s 的前 i 个字符 `s[1..i]` 的解码方法数。

考虑最后一次解码中使用了 s 中的哪些字符:

- 第一种情况是我们使用了一个字符,即 `s[i]` 进行解码,那么只要 `s[i]≠0`,它就可以被解码成 `A∼I` 中的某个字母。由于剩余的前 `i-1` 个字符的解码方法数为 `dp[i-1]`,所以 `dp[i] = dp[i-1]`
- 第二种情况是我们使用了两个字符,即 `s[i-1]``s[i]` 进行编码。与第一种情况类似,`s[i-1]` 不能等于 0,并且 `s[i-1]``s[i]` 组成的整数必须小于等于 26,这样它们就可以被解码成 `J∼Z` 中的某个字母。由于剩余的前 `i-2` 个字符的解码方法数为 `dp[i-2]`,所以 `dp[i] = dp[i-2]`

将上面的两种状态转移方程在对应的条件满足时进行累加,即可得到 `dp[i]`的值。在动态规划完成后,最终的答案即为 `dp[n]`

由于 `dp[i]` 的值仅与 `dp[i-1]``dp[i-2]` 有关,因此可以不定义 dp 数组,可以仅使用三个变量进行状态转移。

<!-- tabs:start -->

### **Python3**

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

```python
class Solution:
def numDecodings(self, s: str) -> int:
n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
if s[i - 1] != '0':
dp[i] += dp[i - 1]
if i > 1 and s[i - 2] != '0' and (int(s[i - 2]) * 10 + int(s[i - 1]) <= 26):
dp[i] += dp[i - 2]
return dp[n]
```

优化空间:

```python
class Solution:
def numDecodings(self, s: str) -> int:
n = len(s)
a, b, c = 0, 1, 0
for i in range(1, n + 1):
c = 0
if s[i - 1] != '0':
c += b
if i > 1 and s[i - 2] != '0' and (int(s[i - 2]) * 10 + int(s[i - 1]) <= 26):
c += a
a, b = b, c
return c
```

### **Java**

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

```java
class Solution {
public int numDecodings(String s) {
int n = s.length();
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
if (s.charAt(i - 1) != '0') {
dp[i] += dp[i - 1];
}
if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0') <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[n];
}
}
```

优化空间:

```java
class Solution {
public int numDecodings(String s) {
int n = s.length();
int a = 0, b = 1, c = 0;
for (int i = 1; i <= n; ++i) {
c = 0;
if (s.charAt(i - 1) != '0') {
c += b;
}
if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0') <= 26) {
c += a;
}
a = b;
b = c;
}
return c;
}
}
```

### **...**
Expand Down
69 changes: 69 additions & 0 deletions solution/0000-0099/0091.Decode Ways/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,83 @@

### **Python3**

Solution1:

```python
class Solution:
def numDecodings(self, s: str) -> int:
n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
if s[i - 1] != '0':
dp[i] += dp[i - 1]
if i > 1 and s[i - 2] != '0' and (int(s[i - 2]) * 10 + int(s[i - 1]) <= 26):
dp[i] += dp[i - 2]
return dp[n]
```

Solution2:

```python
class Solution:
def numDecodings(self, s: str) -> int:
n = len(s)
a, b, c = 0, 1, 0
for i in range(1, n + 1):
c = 0
if s[i - 1] != '0':
c += b
if i > 1 and s[i - 2] != '0' and (int(s[i - 2]) * 10 + int(s[i - 1]) <= 26):
c += a
a, b = b, c
return c
```

### **Java**

Solution1:

```java
class Solution {
public int numDecodings(String s) {
int n = s.length();
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
if (s.charAt(i - 1) != '0') {
dp[i] += dp[i - 1];
}
if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0') <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[n];
}
}
```

Solution2:

```java
class Solution {
public int numDecodings(String s) {
int n = s.length();
int a = 0, b = 1, c = 0;
for (int i = 1; i <= n; ++i) {
c = 0;
if (s.charAt(i - 1) != '0') {
c += b;
}
if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0') <= 26) {
c += a;
}
a = b;
b = c;
}
return c;
}
}
```

### **...**
Expand Down
25 changes: 12 additions & 13 deletions solution/0000-0099/0091.Decode Ways/Solution.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
class Solution {
public int numDecodings(String s) {
int len = s.length();
if (len == 0) return 0;
int current = s.charAt(0) == '0' ? 0 : 1;
int last = 1;
for (int i = 1; i < len; i++) {
int tmp = current;
if(s.charAt(i) == '0'){
if(s.charAt(i-1) == '1' || s.charAt(i-1) == '2') current = last;
else return 0;
}else if(s.charAt(i-1) == '1' || s.charAt(i-1) == '2' && s.charAt(i) <= '6') {
current += last;
int n = s.length();
int a = 0, b = 1, c = 0;
for (int i = 1; i <= n; ++i) {
c = 0;
if (s.charAt(i - 1) != '0') {
c += b;
}
last = tmp;
if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0') <= 26) {
c += a;
}
a = b;
b = c;
}
return current;
return c;
}
}
12 changes: 12 additions & 0 deletions solution/0000-0099/0091.Decode Ways/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def numDecodings(self, s: str) -> int:
n = len(s)
a, b, c = 0, 1, 0
for i in range(1, n + 1):
c = 0
if s[i - 1] != '0':
c += b
if i > 1 and s[i - 2] != '0' and (int(s[i - 2]) * 10 + int(s[i - 1]) <= 26):
c += a
a, b = b, c
return c

0 comments on commit 90b3883

Please sign in to comment.