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 bc3a0b4 commit df74dd4Copy full SHA for df74dd4
โdecode-ways/easyone-jwlee.goโ
@@ -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