일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- FIT XR
- 10분
- Daily Challenge
- 운동
- 괜찮음
- English
- 미드시청
- 쓰릴오브파이트
- 링피트
- 잡생각
- 리얼 클래스
- 3줄정리
- leetcode
- 프로젝트
- 개발자
- 매일
- 스탭퍼
- 읽기
- 30분
- 영어원서읽기
- 월간
- 영어공부
- Writing
- 파비최
- Problem Solving
- 만화도
- 사이드
- 뭐든
- 화상영어
- realclass
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/numbers-with-same-consecutive-differences/
Numbers With Same Consecutive Differences - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
요즘에 DFS/BFS 문제가 주로 나오는 듯.
class Solution:
def numsSameConsecDiff(self, n: int, k: int) -> List[int]:
if n == 1:
return [i for i in range(10)]
ans = []
def dfs(n, num):
if n == 0:
return ans.append(num)
tail_digit = num % 10
next_digits = set([tail_digit + k, tail_digit - k])
for next_digit in next_digits:
if 0 <= next_digit < 10:
new_num = num * 10 + next_digit
dfs(n-1, new_num)
for num in range(1, 10):
dfs(n-1, num)
return list(ans)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.09.05 |
---|---|
Today's Challenge (0) | 2022.09.04 |
Today's Challenge (0) | 2022.09.02 |
Today's Challenge (0) | 2022.09.01 |
Today's Challenge (0) | 2022.08.31 |
Comments