[LeetCode]400. Nth Digit(规律)
2017-03-14 20:08
295 查看
题目链接:https://leetcode.com/problems/nth-digit/#/description
题意:找1234567891011...这样的字符串中第n位是数字几。
规律就是k位数有9*10^k个数字,找到第n位数所在的数字区段,记作k位数的段,然后在这个段里找到对应的那个k位数,然后数出来对应的位就行了。
题意:找1234567891011...这样的字符串中第n位是数字几。
规律就是k位数有9*10^k个数字,找到第n位数所在的数字区段,记作k位数的段,然后在这个段里找到对应的那个k位数,然后数出来对应的位就行了。
1 class Solution { 2 public: 3 int findNthDigit(int n) { 4 if(n == 0) return 0; 5 long long len = 1, cnt = 9, s = 1; 6 while(n > len * cnt) { 7 n -= len * cnt; 8 len++; 9 cnt *= 10; 10 s *= 10; 11 } 12 s += (n - 1) / len; 13 char buf[32]; 14 sprintf(buf, "%d", s); 15 return buf[(n-1)%len] - '0'; 16 } 17 };
相关文章推荐
- leetcode 400. Nth Digit 从1到n第n位数字是什么 + 找规律
- 【Leetcode】400. Nth Digit
- 【LeetCode】 400. Nth Digit
- 【C语言】 LeetCode 400. Nth Digit
- LeetCode - 400. Nth Digit
- 【Leetcode】400. Nth Digit
- LeetCode 400. Nth Digit
- LeetCode 400. Nth Digit
- Leetcode 400. Nth Digit[easy]
- [LeetCode]--400. Nth Digit
- LeetCode-400. Nth Digit
- LeetCode-400. Nth Digit
- LeetCode 400. Nth Digit
- [leetcode] 400. Nth Digit 解题报告
- LeetCode400. Nth Digit
- leetcode 400. Nth Digit解题报告
- leetcode 233. Number of Digit One 从1到n的数组中出现数字1的数量 + 寻找规律,公式计算
- Leetcode 400. Nth Digit 第n个数字 解题报告
- LeetCode 400. Nth Digit
- leetcode 400. Nth Digit