Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca9457f

Browse files
committedJul 8, 2024
week11 mission decode-ways
1 parent 4d7879e commit ca9457f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
 

β€Ždecode-ways/dev-jonghoonpark.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
졜근의 데이터가 μ•„λ‹Œ 이전 데이터듀은 더 이상 μ°Έμ‘°λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— ν•„μš”ν•œ κ³΅κ°„λ§Œ λ§Œλ“€μ–΄μ„œ λ³΄κ΄€ν•˜λ©΄ λœλ‹€.

0 commit comments

Comments
 (0)
Please sign in to comment.