Skip to content

Commit df74dd4

Browse files
author
easyone
committed
Feat: Add solution of decode-ways
1 parent bc3a0b4 commit df74dd4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// ํ’€์ด
2+
// dp๋กœ ํ’€์ด
3+
// ๋‘์ž๋ฆฌ ์ˆ˜์— ์ ํ•ฉํ•˜๋ฉด prev2(i-2)์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋”ํ•˜๊ธฐ
4+
5+
// TC
6+
// O(n)
7+
8+
// SC
9+
// data type์ด int์ธ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ O(1)
10+
11+
func numDecodings(s string) int {
12+
if len(s) == 0 || s[0] == '0' {
13+
return 0
14+
}
15+
prev2, prev1 := 1, 1
16+
for i := 1; i < len(s); i++ {
17+
curr := 0
18+
19+
// ํ•œ์ž๋ฆฌ์ˆ˜ ํ™•์ธ
20+
if s[i] != '0' {
21+
curr += prev1
22+
}
23+
24+
// ๋‘์ž๋ฆฌ์ˆ˜ ํ™•์ธ
25+
digit, _ := strconv.Atoi(s[i-1 : i+1])
26+
if digit >= 10 && digit <= 26 {
27+
curr += prev2
28+
}
29+
30+
prev2, prev1 = prev1, curr
31+
}
32+
return prev1
33+
}

0 commit comments

Comments
ย (0)