File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+
4+ public List <Integer > nums ;
5+ public int numDecodings (String s ) {
6+ nums = new ArrayList <>();
7+ for (int i = 0 ; i < s .length (); i ++) {
8+ nums .add (s .charAt (i ) - 48 );
9+ }
10+
11+ int [] memo = new int [nums .size () + 1 ];
12+ Arrays .fill (memo , -1 );
13+ return dfs (0 , memo );
14+ }
15+
16+ public int dfs (int start , int [] memo ) {
17+
18+ if (start == nums .size ()) return 1 ;
19+
20+ if (memo [start ] != -1 ) {
21+ return memo [start ];
22+ }
23+ if (nums .get (start ) == 0 ) {
24+ memo [start ] = 0 ;
25+ }
26+ else if (start + 1 < nums .size () && nums .get (start ) * 10 + nums .get (start + 1 ) < 27 ) {
27+ memo [start ] = dfs (start + 1 , memo ) + dfs (start + 2 , memo );
28+ } else {
29+ memo [start ] = dfs (start + 1 , memo );
30+ }
31+ return memo [start ];
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments