题目描述:
给你一个整数 n ,请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...] 中找出并返回第 n 位上的数字。
方法一:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26class Solution { public: int findNthDigit(int n) { long long curn = 0; int x = 10; int count = 1; for (int i = 1; ; i++) { if (i == x) { x *= 10; count++; } curn += count; if (curn >= n) { curn -= count; stringstream ss; ss << i; string temp; ss >> temp; return (temp[n - curn - 1] - '0'); } } } };
前几天的每日一题,注意用int类型的话会溢出,所以curn用long long。
最后
以上就是体贴酒窝最近收集整理的关于2021.12.1 力扣-每日一题-第N位数字的全部内容,更多相关2021.12.1内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复