题解 | #选牛比赛#
选牛比赛
https://www.nowcoder.com/practice/75df501809d14532a00e747666587873
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param digits string字符串一维数组
# @param n int整型
# @return int整型
#
class Solution:
def countEligibleCows(self, digits: List[str], n: int) -> int:
# write code here
digit = [int(d) for d in digits]
limit = [int(d) for d in str(n)]
if len(limit) == 1: return sum(d<=limit[0] for d in digit)
q = len(digit)
num = q*(q**(len(limit)-1)-1)//(q-1) # a0 * (q^(n-1)-1)/(q-1)
for i in range(len(limit)):
opt = sum(d<limit[i] for d in digit)
num += opt * q*(q**(len(limit)-i-1)-1)//(q-1)
if limit[i] not in digit: break
return num


查看10道真题和解析