数位dp 2376, 3791
We call a positive integer special if all of its digits are distinct.
Given a positive integer n, return the number of special integers that belong to the interval [1, n].
Example 1:
Input: n = 20 Output: 19 Explanation: All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.
Example 2:
Input: n = 5 Output: 5 Explanation: All the integers from 1 to 5 are special.
Example 3:
Input: n = 135 Output: 110 Explanation: There are 110 integers from 1 to 135 that are special. Some of the integers that are not special are: 22, 114, and 131.
Constraints:
1 <= n <= 2 * 109
class Solution: ''' 对各个数位逐位进行填充 是否已经开始填 如果还没开始填,选择不填(相当于会是更少位数),可以从下一位开始 如果选择填: 1.决定可以填充的范围 low: 如果还没开始填,那么只能从1开始填,否则可从0开始 up: a.如果前面各位都是上限,那么这位只能取到s[i] b.否则可以取到9 2.轮询low~up 如果之前没有填过的数字就可以用 另外记得更新mask和是否上限 ''' def countSpecialNumbers(self, n: int) -> int: s = str(n) @cache # 缓存装饰器,避免重复计算 dfs 的结果(记忆化) def dfs(i: int, mask: int, is_limit: bool, is_num: bool) -> int: # print(sub, i, bin(mask)[2:], is_limit, is_num) if i == len(s): return 1 if is_num else 0 # is_num 为 True 表示得到了一个合法数字 res = 0 # 还没开始填,可以跳过当前数位 if not is_num: res = dfs(i + 1, mask, False, False) # low 和 up 决定当前数位的取值范围 # 如果前面没有填数字,则必须从 1 开始(因为不能有前导零) low = 0 if is_num else 1 ''' # 如果前面填的数字都和 n 的一样,那么这一位至多填 s[i](否则就超过 n 啦),比如: 234 当前在3这位,如果之前一位填的是1,那么这一位随便填,如果之前一位填的是2,那么这一位只能填小于3的数字 ^ ''' up = int(s[i]) if is_limit else 9 for d in range(low, up + 1): # 枚举要填入的数字 d if mask >> d & 1 == 0: # d 不在 mask 中,说明之前没有填过 d res += dfs(i + 1, mask | (1 << d), is_limit and d == up, True) return res # is_limit 初始值为True # is_num 初始值为False return dfs(0, 0, True, False)
low and high.An integer is called balanced if it satisfies both of the following conditions:
- It contains at least two digits.
- The sum of digits at even positions is equal to the sum of digits at odd positions (the leftmost digit has position 1).
Return an integer representing the number of balanced integers in the range [low, high] (both inclusive).
Example 1:
Input: low = 1, high = 100
Output: 9
Explanation:
The 9 balanced numbers between 1 and 100 are 11, 22, 33, 44, 55, 66, 77, 88, and 99.
Example 2:
Input: low = 120, high = 129
Output: 1
Explanation:
Only 121 is balanced because the sum of digits at even and odd positions are both 2.
Example 3:
Input: low = 1234, high = 1234
Output: 0
Explanation:
1234 is not balanced because the sum of digits at odd positions (1 + 3 = 4) does not equal the sum at even positions (2 + 4 = 6).
Constraints:
1 <= low <= high <= 1015
class Solution: def countBalanced(self, low: int, high: int) -> int: s_high = str(high) # 前补0 比如:"001" s_low = str(low).zfill(len(s_high)) @cache # i:当前数位 is_down_limit:是否为下限 is_up_limit:是否为上限 diff:奇偶位和差异 def dfs(i, is_down_limit, is_up_limit, diff): if i == len(s_high): # print(s, odd, even, odd == even) return 1 if diff == 0 else 0 res = 0 start = int(s_low[i]) if is_down_limit else 0 end = int(s_high[i]) if is_up_limit else 9 for j in range(start, end + 1): res += dfs( i + 1, is_down_limit and j == start, is_up_limit and j == end, diff + j * (1 if i % 2 == 0 else -1) ) return res return dfs(0, True, True, 0)

浙公网安备 33010602011771号