forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeWays.java
More file actions
32 lines (26 loc) · 1.02 KB
/
DecodeWays.java
File metadata and controls
32 lines (26 loc) · 1.02 KB
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
import java.util.Hashtable;
public class Solution {
public int numDecodings(String s) {
// Start typing your Java solution below
// DO NOT write main() function
Hashtable<String,Integer> hash = new Hashtable<String,Integer>();
return gnumDecodings(s,hash);
}
public int gnumDecodings(String s, Hashtable<String,Integer> hash) {
int len = s.length();
if(len == 0 || s.charAt(0) == '0') return 0;
if(len == 1) return 1;
if(hash.containsKey(s))
return hash.get(s);
int i = Integer.parseInt(s.substring(0,2));
int temp;
if(i>26)
temp = gnumDecodings(s.substring(1),hash);
else if(len == 2)
temp = 1+gnumDecodings(s.substring(1),hash);
else
temp = gnumDecodings(s.substring(1),hash) + gnumDecodings(s.substring(2),hash);
hash.put(s, temp);
return temp;
}
}