题解 | #数字序列中某一位的数字 M#
数字序列中某一位的数字
https://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
#include <string> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 */ int findNthDigit(int n) { // write code here int digit = 1; long start = 1; long sum = 9; while (n > sum) { n -= sum; start *=10; digit ++; sum = 9 * start * digit; } int num = start + (n-1)/digit; int index = (n-1) % digit; return to_string(num)[index] - '0'; } };
先 mark
note_coding 文章被收录于专栏
记录自己的解题思路, 欢迎评价