File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ - λ¬Έμ : https://leetcode.com/problems/decode-ways/
2
+ - νμ΄: https://algorithm.jonghoonpark.com/2024/07/08/leetcode-91
3
+
4
+ ``` java
5
+ class Solution {
6
+ public int numDecodings (String s ) {
7
+ int [] dp = new int [s. length()];
8
+
9
+ if (s. charAt(0 ) == ' 0' ) {
10
+ return 0 ;
11
+ }
12
+ dp[0 ] = 1 ;
13
+
14
+ for (int i = 1 ; i < s. length(); i++ ) {
15
+ int oneDigit = Integer . parseInt(String . valueOf(s. charAt(i)));
16
+ if (oneDigit > 0 ) {
17
+ dp[i] = dp[i - 1 ];
18
+ }
19
+
20
+ int prevDigit = Integer . parseInt(String . valueOf(s. charAt(i - 1 )));
21
+ if (prevDigit == 0 ) {
22
+ continue ;
23
+ }
24
+
25
+ int twoDigit = prevDigit * 10 + oneDigit;
26
+ if (twoDigit <= 26 ) {
27
+ if (i > 2 ) {
28
+ dp[i] = dp[i] + dp[i - 2 ];
29
+ } else {
30
+ dp[i] = dp[i] + 1 ;
31
+ }
32
+ }
33
+ }
34
+
35
+ return dp[s. length() - 1 ];
36
+ }
37
+ }
38
+ ```
39
+
40
+ ### TC, SC
41
+
42
+ μκ° λ³΅μ‘λλ O(n), κ³΅κ° λ³΅μ‘λλ O(n)μ΄λ€.
43
+ μ΄λ°μμΌλ‘ μ΅κ·Ό λ°μ΄ν°λ§ μ¬μ¬μ© νλ κ²½μ°μλ 곡κ°λ³΅μ‘λλ₯Ό O(1) μΌλ‘λ μ€μΌ μ μμ κ²μ΄λ€.
44
+ μ΅κ·Όμ λ°μ΄ν°κ° μλ μ΄μ λ°μ΄ν°λ€μ λ μ΄μ μ°Έμ‘°λμ§ μκΈ° λλ¬Έμ νμν 곡κ°λ§ λ§λ€μ΄μ 보κ΄νλ©΄ λλ€.
You canβt perform that action at this time.
0 commit comments