求助|求指点,902题
467
2023.09.16
发布于 未知归属地

第902题最大为N的数字组合,为什么转化为整数判断就过不了,求大佬点我一下
为什么这样过不了

class Solution:
   def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
       s = str(n)
       @cache
       def f(i,is_limit,is_num):
           if i == len(s):
               return int(is_num)
           res = 0
           if not is_num:
               res = f(i + 1,False,False)
           up = int(s[i]) if is_limit else 9
           for d in digits:
               if int(d) > up:
                   break
               res += f(i + 1,is_limit and d == up,True)
           return res
       return f(0,True,False)

但是这样就过得了?

class Solution:
   def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:
       s = str(n)
       @cache
       def f(i,is_limit,is_num):
           if i == len(s):
               return int(is_num)
           res = 0
           if not is_num:
               res = f(i + 1,False,False)
           up = s[i] if is_limit else "9"
           for d in digits:
               if d > up:
                   break
               res += f(i + 1,is_limit and d == up,True)
           return res
       return f(0,True,False)

我好像陷进某个陷阱但是到现在还没反应过来,求大佬点播我一下

评论 (2)