일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- t1
- 315. Count of Smaller Numbers After Self
- attribute
- Regular Expression
- Generator
- data science
- Python Code
- 시바견
- DWG
- Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- concurrency
- Protocol
- Python
- shiba
- iterator
- LeetCode
- 컴퓨터의 구조
- 715. Range Module
- Decorator
- Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- kaggle
- 파이썬
- Class
- Python Implementation
- 운영체제
- 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 |