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