@@ -24,15 +24,21 @@ public class _91 {
2424 * In the end, dp[n] will be the end result.*/
2525
2626 public static int numDecodings_solution2 (String s ) {
27- if (s == null || s .length () == 0 ) return 0 ;
27+ if (s == null || s .length () == 0 ) {
28+ return 0 ;
29+ }
2830 int [] dp = new int [s .length ()+1 ];
2931 dp [0 ] = 1 ;
3032 dp [1 ] = (s .charAt (0 ) != '0' ) ? 1 : 0 ;
3133 for (int i = 2 ; i <= s .length (); i ++){
3234 int first = Integer .valueOf (s .substring (i -1 ,i ));
3335 int second = Integer .valueOf (s .substring (i -2 , i ));
34- if (first > 0 && first <= 9 ) dp [i ] += dp [i -1 ];
35- if (second >= 10 && second <= 26 ) dp [i ] += dp [i -2 ];
36+ if (first > 0 && first <= 9 ) {
37+ dp [i ] += dp [i -1 ];
38+ }
39+ if (second >= 10 && second <= 26 ) {
40+ dp [i ] += dp [i -2 ];
41+ }
3642 }
3743 return dp [s .length ()];
3844 }
@@ -44,7 +50,9 @@ public static void main(String...args){
4450 /**My original accepted yet lengthy solution.*/
4551 public static int numDecodings_solution1 (String s ) {
4652
47- if (s == null || s .length () == 0 ) return 0 ;
53+ if (s == null || s .length () == 0 ) {
54+ return 0 ;
55+ }
4856 Set <String > validStrings = new HashSet ();
4957 validStrings .add ("1" );
5058 validStrings .add ("2" );
@@ -75,8 +83,11 @@ public static int numDecodings_solution1(String s) {
7583
7684 int n = s .length ();
7785 int [] dp = new int [n ];
78- if (validStrings .contains (s .substring (0 ,1 ))) dp [0 ] = 1 ;
79- else dp [0 ] = 0 ;
86+ if (validStrings .contains (s .substring (0 ,1 ))) {
87+ dp [0 ] = 1 ;
88+ } else {
89+ dp [0 ] = 0 ;
90+ }
8091
8192 for (int i = 1 ; i < n ; i ++){
8293 if (validStrings .contains (s .substring (i ,i +1 )) && validStrings .contains (s .substring (i -1 ,i +1 ))) {
@@ -88,13 +99,15 @@ public static int numDecodings_solution1(String s) {
8899 } else if (!validStrings .contains (s .substring (i ,i +1 )) && !validStrings .contains (s .substring (i -1 ,i +1 ))){
89100 return 0 ;
90101 } else if (!validStrings .contains (s .substring (i ,i +1 )) && validStrings .contains (s .substring (i -1 ,i +1 ))){
91- if (i > 1 ) dp [i ] = dp [i -2 ];
92- else dp [i ] = dp [i -1 ];
93- } else dp [i ] = dp [i -1 ];
102+ if (i > 1 ) {
103+ dp [i ] = dp [i -2 ];
104+ } else {
105+ dp [i ] = dp [i -1 ];
106+ }
107+ } else {
108+ dp [i ] = dp [i - 1 ];
109+ }
94110 }
95-
96111 return dp [n -1 ];
97-
98112 }
99-
100113}
0 commit comments