일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 만화도
- English
- 스탭퍼
- Daily Challenge
- 잡생각
- FIT XR
- 링피트
- 사이드
- 리얼 클래스
- 3줄정리
- 월간
- 매일
- 뭐든
- 영어공부
- 쓰릴오브파이트
- leetcode
- realclass
- Writing
- 화상영어
- 개발자
- 읽기
- 괜찮음
- 10분
- 미드시청
- 프로젝트
- 파비최
- 운동
- Problem Solving
- 영어원서읽기
- 30분
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.12.26 Today's Challenge 본문
https://leetcode.com/problems/decode-ways/
Decode Ways - LeetCode
Can you solve this real interview question? Decode Ways - A message containing letters from A-Z can be encoded into numbers using the following mapping: 'A' -> "1" 'B' -> "2" ... 'Z' -> "26" To decode an encoded message, all the digits must be grouped then
leetcode.com
DP로 푸는 문제
class Solution:
def numDecodings(self, s: str) -> int:
if s == "0":
return 0
dp_2 = 1
dp_1 = int(s[-1] != "0")
i = len(s) - 2
while i >= 0:
if s[i] == "0":
dp_0 = 0
else:
dp_0 = dp_1
if (s[i] == "1") or (s[i] == "2" and int(s[i+1]) < 7):
dp_0 += dp_2
i -= 1
dp_0, dp_1, dp_2 = 0, dp_0, dp_1
return dp_1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.12.28 Today's Challenge (0) | 2023.12.28 |
---|---|
2023.12.27 Today's Challenge (0) | 2023.12.27 |
2023.12.25 Today's Challenge (1) | 2023.12.25 |
2023.12.24 Today's Challenge (0) | 2023.12.24 |
2023.12.23 Today's Challenge (1) | 2023.12.23 |
Comments