-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode-ways.rs
57 lines (49 loc) · 1.38 KB
/
decode-ways.rs
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
#![allow(dead_code, unused, unused_variables)]
fn main() {
println!("{}", Solution::num_decodings("12".to_string()));
println!("{}", Solution::num_decodings("06".to_string()));
println!(
"{}",
Solution::num_decodings("2611055971756562".to_string())
);
println!(
"{}",
Solution::num_decodings("111111111111111111111111111111111111111111111".to_string())
);
}
struct Solution;
impl Solution {
pub fn num_decodings(s: String) -> i32 {
if s.starts_with('0') {
return 0;
}
let mut v = vec![-1; s.len()];
Self::f(s.as_bytes(), &mut v)
}
fn f(s: &[u8], v: &mut Vec<i32>) -> i32 {
let mut r = 0;
let l = s.len();
if l > 0 {
if v[l - 1] != -1 {
r = v[l - 1];
} else {
if s[0] != b'0' {
if l > 1 {
if (s[0] == b'2' && s[1] <= b'6') || s[0] < b'2' {
r += Self::f(&s[2..], v);
}
if s[1] != b'0' {
r += Self::f(&s[1..], v);
}
} else {
r += Self::f(&s[1..], v);
}
}
}
v[l - 1] = r;
} else {
r = 1;
}
r
}
}