數字序列中某一位的數字

2021-10-04 00:07:19 字數 714 閱讀 3334

數字以0123456789101112131415…的格式序列化到乙個字串行中。在這個序列中,第5位(從下標0開始計數)是5,第13位是1,第19位是4,等等。

請寫乙個函式,求任意第n位對應的數字。

示例 1:

示例 2:

限制:0 <= n < 2^31

1.確定第n位是幾位數:

每個位數對應有9*pow(10,digits-1)*digits個數。

2.確定是該位數的第幾位:

pow(10,digits-1) + n/digits;

3.位數取餘得到是數字上的第幾位。

class

solution

int digits =1;

//代表數字的位數

while

(n >9*

pow(

10,digits-1)

*digits)

//得到n是代表的數字對應的第幾個數

int res =

pow(

10,digits-1)

+ n/digits;

int mod = n % digits;

if(mod ==0)

else}}

;

數字序列中某一位的數字

面試題44 數字序列中某一位的數字 題目 數字以0123456789101112131415 的格式序列化到乙個字串行中。在這 個序列中,第5位 從0開始計數 是5,第13位是1,第19位是4,等等。請寫一 個函式求任意位對應的數字。static int digit at uint32 t inde...

數字序列中某一位的數字

012345678910.序列中,第n位數 def solution n if n 0 return 1 if n 10 return n i 2 確定是第幾位數 sums 10 ret 0while true 一位數 0 9 共10個字元 兩位數 10 99 共180個字元 三位數 100 999...

數字序列中某一位的數字

數字以0123456789101112131415 的格式序列化到乙個字串行中,在這個序列中。第五位 從0開始計數 是5,第13位是1,第19位是4,等等。請寫乙個函式,求任意第n位對應的數字。暴力解法,從0開始計數,數到第n位。以1001位為例子。因為序列前10位是0 9這10個只有1位的數字。序...