일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Python Code
- t1
- shiba
- Class
- concurrency
- Protocol
- Convert Sorted List to Binary Search Tree
- attribute
- 715. Range Module
- Python Implementation
- Decorator
- 파이썬
- kaggle
- 프로그래머스
- Python
- Substring with Concatenation of All Words
- 컴퓨터의 구조
- Generator
- 30. Substring with Concatenation of All Words
- data science
- DWG
- 109. Convert Sorted List to Binary Search Tree
- 시바견
- LeetCode
- iterator
- 315. Count of Smaller Numbers After Self
- Regular Expression
- 43. Multiply Strings
- 밴픽
- 운영체제
Archives
- Today
- Total
Scribbling
LeetCode: 91. Decode Ways 본문
class Solution:
def numDecodings(self, s: str) -> int:
candidates = set([str(i) for i in range(1, 27)])
# dp[i] denotes # of ways until s[i-1]
dp = [0] * (len(s) + 1)
dp[0] = 1
if s[0] in candidates:
dp[1] = 1
for i in range(2, len(s)+1):
# dp[n] = dp[n-2] * k2 + dp[n-1] * k1
k2 = 0
if s[i-2:i] in candidates:
k2 += 1
k1 = 0
if s[i-1] in candidates:
k1 += 1
dp[i] = dp[i-2] * k2 + dp[i-1] * k1
return dp[len(s)]
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 97. Interleaving String (0) | 2021.10.11 |
---|---|
LeetCode: 93. Restore IP Addresses (0) | 2021.10.11 |
LeetCode: 100. Same Tree (0) | 2021.10.11 |
LeetCode: 90. Subsets II (0) | 2021.10.10 |
LeetCode: 88. Merge Sorted Array (0) | 2021.10.09 |