File tree Expand file tree Collapse file tree 1 file changed +15
-7
lines changed Expand file tree Collapse file tree 1 file changed +15
-7
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int numDecodings (String s ) {
3
+ if (s == null || s .length () == 0 ) {
4
+ return 0 ;
5
+ }
6
+
3
7
int [] dp = new int [s .length () + 1 ];
4
- dp [0 ] = 1 ;
5
- dp [1 ] = s .charAt (0 ) != '0' ? 1 : 0 ;
6
8
7
- for (int i = 2 ; i <= s .length (); i ++) {
8
- int oneDigit = Integer .valueOf (s .substring (i - 1 , i ));
9
- int twoDigits = Integer .valueOf (s .substring (i - 2 , i ));
9
+ if (s .charAt (0 ) != '0' ) {
10
+ dp [0 ] = 1 ;
11
+ }
12
+ if (dp [0 ] == 1 ) {
13
+ dp [1 ] = 1 ;
14
+ }
10
15
11
- if (1 <= oneDigit && oneDigit <= 9 ) {
16
+ for (int i = 2 ; i <= s .length (); i ++) {
17
+ if (s .charAt (i - 1 ) != '0' ) {
12
18
dp [i ] += dp [i - 1 ];
13
19
}
14
20
15
- if (10 <= twoDigits && twoDigits <= 26 ) {
21
+ int twoDigits = Integer .valueOf (s .substring (i - 2 , i ));
22
+
23
+ if (twoDigits >= 10 && twoDigits <= 26 ) {
16
24
dp [i ] += dp [i - 2 ];
17
25
}
18
26
}
You can’t perform that action at this time.
0 commit comments