-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path091_DecodeWays91.java
141 lines (118 loc) · 3.71 KB
/
091_DecodeWays91.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* A message containing letters from A-Z is being encoded to numbers using the
* following mapping:
*
* 'A' -> 1
* 'B' -> 2
* ...
* 'Z' -> 26
* Given an encoded message containing digits, determine the total number of
* ways to decode it.
*
* For example,
* Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
*
* The number of ways decoding "12" is 2.
*/
import java.util.Map;
import java.util.HashMap;
public class DecodeWays91 {
public int numDecodings(String s) {
int length = s.length();
length = s.length();
if (length == 0) {
return 0;
}
if (s.charAt(0) == '0') {
return 0;
}
int[] dp = new int[length + 1];
dp[0] = 1;
dp[1] = 1;
boolean isZero = false;
for (int i = 2; i <= length; i++) {
int n = Integer.valueOf(s.substring(i-2, i));
if (s.charAt(i - 1) == '0' && (n > 26 || n == 0)) {
return 0;
} else if (s.charAt(i - 1) == '0') {
isZero = true;
dp[i] = dp[i - 2];
} else if (isZero) {
isZero = false;
dp[i] = dp[i - 1];
} else {
isZero = false;
dp[i] = dp[i - 1] + ((n > 0 && n <= 26) ? dp[i - 2] : 0);
}
}
return dp[length];
}
/**
* https://discuss.leetcode.com/topic/2562/dp-solution-java-for-reference
*/
public int numDecodings2(String s) {
int n = s.length();
if (n == 0) return 0;
int[] memo = new int[n+1];
memo[n] = 1;
memo[n-1] = s.charAt(n-1) != '0' ? 1 : 0;
for (int i = n - 2; i >= 0; i--)
if (s.charAt(i) == '0') continue;
else memo[i] = (Integer.parseInt(s.substring(i,i+2))<=26) ? memo[i+1]+memo[i+2] : memo[i+1];
return memo[0];
}
/**
* https://discuss.leetcode.com/topic/35840/java-clean-dp-solution-with-explanation
*/
public int numDecodings3(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int[] ways = new int[s.length() + 1];
ways[0] = 1;
ways[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i <= s.length(); i++) {
int one = s.charAt(i - 1) - '0';
int two = (s.charAt(i - 2) - '0') * 10 + one;
if (one != 0) {
ways[i] += ways[i - 1];
}
if (two >= 10 && two <= 26) {
ways[i] += ways[i - 2];
}
}
return ways[s.length()];
}
public int numDecodings4(String s) {
if (s == null || s.length() == 0) return 0;
if (s.charAt(0) == '0') return 0;
int[] dp = new int[s.length()+1];
dp[0] = 1;
dp[1] = 1;
for (int i=2; i<=s.length(); i++) {
int now = s.charAt(i-1) - '0';
int prenow = (s.charAt(i-2) - '0')*10 + now;
if (now == 0 && prenow != 10 && prenow != 20) return 0;
int a = now == 0 ? 0 : dp[i-1];
int b = (prenow >= 10 && prenow <= 26) ? dp[i-2] : 0;
dp[i] = a + b;
}
return dp[s.length()];
}
/**
* https://leetcode.com/problems/decode-ways/discuss/30416/Simple-java-solution-with-O(1)-space
*/
public int numDecodings5(String s) {
int n1 =1, n2=1, n3=0;
if(s.length() == 0 || s.charAt(0) == '0') return 0;
for (int i=2; i<=s.length(); i++) {
n3=0;
if(s.charAt(i-1)!='0') n3=n2;
int num = Integer.parseInt(s.substring(i-2,i));
if(num>=10 && num<=26) n3+=n1;
n1=n2;
n2=n3;
}
return n2;
}
}