Skip to content

Commit d153a84

Browse files
authored
neetcode 솔루션
1 parent 100a436 commit d153a84

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

decode-ways/yeonguchoe.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)