We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 100a436 commit d153a84Copy full SHA for d153a84
decode-ways/yeonguchoe.go
@@ -0,0 +1,32 @@
1
+func dfs(s string, i int, memo map[int]int) int {
2
+ if i == len(s) {
3
+ return 1
4
+ }
5
+ if val, found := memo[i]; found {
6
+ return val
7
8
+ if s[i] == '0' {
9
+ memo[i] = 0
10
+ return 0
11
12
+
13
+ // 1자리 숫자로 디코딩
14
+ result := dfs(s, i+1, memo)
15
16
+ // 2자리 숫자로 디코딩
17
+ if i+1 < len(s) {
18
+ // 10의 자리 숫자 또는 20의 자리 숫자로 디코딩
19
+ if (s[i] == '1') || (s[i] == '2' && s[i+1] >= '1' && s[i+1] <= '6') {
20
+ result += dfs(s, i+2, memo)
21
22
23
+ // dp에 저장
24
+ memo[i] = result
25
+ return result
26
+}
27
28
+func numDecodings(s string) int {
29
+ // memo 테이블 만들고 dfs 호출
30
+ memo := make(map[int]int)
31
+ return dfs(s, 0, memo)
32
0 commit comments