数位DP
leetcode 2376
模板题,用记忆化搜索实现会更加直观,需要知道的信息有:
1. 当前到第几位
2. 已选集合,避免重复
3. 是否到上限,到不到上限在同一位上可选范围是不一样的
4. 是否开始计数(考虑前导0)
class Solution:
def countSpecialNumbers(self, n: int) -> int:
s = str(n)
@cache
def dfs(i, mask, is_limit, is_num):
if i == len(s):