forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_Ways.cpp
More file actions
95 lines (85 loc) · 2.36 KB
/
Decode_Ways.cpp
File metadata and controls
95 lines (85 loc) · 2.36 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
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
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: June 27, 2012
Problem: Decode ways
Difficulty: medium
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
I think the problem has a dynamic programming solution. Assume f(n) is the
number of ways decoding left n characters of string s. Then we can get
f[n] = f[n-1] + f[n-2], if both s[n-1,n] and s[n] can be decoded;
= f[n-1], if s[n-1,n] can't be decoded, s[n] can be decoded;
= f[n-2], if s[n-1,n] can be decode,, but s[n] can't be decoded;
= 0, if neither s[n-1,n] and s[n] can be decoded.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
bool canDecode(string num) {
if (num.length() == 1) {
if (num[0] == '0')
return false;
return true;
}
else {
if (num[0] != '1' && num[0] != '2')
return false;
int value;
stringstream(num) >> value;
if (value > 26)
return false;
return true;
}
}
int numDecodings(string s) {
int f[s.length() + 1];
int value;
if (s.empty()) {
return 0;
}
f[0] = 1;
if (canDecode(s.substr(0, 1))) {
f[1] = 1;
}
else {
f[1] = 0;
}
for (int i = 1; i < s.length(); i++) {
bool a = canDecode(s.substr(i - 1, 2));
bool b = canDecode(s.substr(i, 1));
if (a && b) {
f[i + 1] = f[i] + f[i - 1];
}
else if (!a && b) {
f[i + 1] = f[i];
}
else if (a && !b) {
f[i + 1] = f[i - 1];
}
else {
f[i + 1] = 0;
}
}
return f[s.length()];
}
};