Skip to content

Commit 3ef5a41

Browse files
committed
풀이1: Decode Ways #268
1 parent af374c9 commit 3ef5a41

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

decode-ways/seungseung88.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
var numDecodings = function (s) {
6+
const memo = new Map();
7+
memo.set(s.length, 1);
8+
9+
function dfs(start) {
10+
if (memo.has(start)) {
11+
return memo.get(start);
12+
}
13+
14+
if (s[start] === '0') {
15+
memo.set(start, 0);
16+
} else if (start + 1 < s.length && parseInt(s.slice(start, start + 2)) < 27) {
17+
memo.set(start, dfs(start + 1) + dfs(start + 2));
18+
} else {
19+
memo.set(start, dfs(start + 1));
20+
}
21+
22+
return memo.get(start);
23+
}
24+
25+
return dfs(0);
26+
};

0 commit comments

Comments
 (0)