【每日一题20220518】某位数字

:mage:‍ 数字以 0123456789101112131415… 的格式作为一个字符序列,在这个序列中第2位(从下标 0 开始计算)是 2,第10位是1,第13位是1,以此类题,请你输出第n位对应的数字。 数据范围: 0<=n<=10^9

【示例】
输入:2
输出:2
解释:索引是2的位置上的数字是2

题目难度:简单
题目来源:牛客网-数字序列中某一位的数字

def solution(n: int)-> int:
    # your code here

assert solution(0) == 0
assert solution(2) == 2
assert solution(10) == 1
assert solution(13) == 1
def solution(n: int)-> int:
    # your code here
    return int(''.join([str(x) for x in range(n+1)])[n])

assert solution(0) == 0
assert solution(2) == 2
assert solution(10) == 1
assert solution(13) == 1
def solution(n: int)-> int:
    # your code here
    return int("".join(str(i) for i in range(n+1))[n]) if 0 <= n <= 10**9 else -1


assert solution(0) == 0
assert solution(2) == 2
assert solution(10) == 1
assert solution(13) == 1

def solution(n: int)-> int:
# your code here
allnum = ‘’
for i in range(n+1):
allnum += f"{i}"
return int(allnum[n])

def solution(n: int)-> int:
    if(0<=n<=10**9):
        a=""
        for i in range(0,n+1):
            a=a+str(i)
        num=int(a[n])
    else:
        num=-1
    return num

assert solution(0) == 0
assert solution(2) == 2
assert solution(10) == 1
assert solution(13) == 1
def solution(n: int)-> int:
    return ''.join([str(i)for i in range(0,n+1)])[n]
def solution(n: int)-> int:
    digit_count = 1
    min = 0
    max = 10
    while n>= (max -min)*digit_count:
        n = n - (max -min)*digit_count
        min = max
        max = max*10
        digit_count =digit_count +1
    return int(str(n//digit_count+min)[n%digit_count])
def solution(n: int)-> int:
    # your code here
    # l = [str(a) for a in range(n+1)]
    # s = ''.join(l)
    # i = int(s[n])
    return int(''.join([str(a) for a in range(n+1)])[n])


assert solution(0) == 0
assert solution(2) == 2
assert solution(10) == 1
assert solution(13) == 1
关闭