LeetCode-400. Nth Digit
2018-02-26 13:34
435 查看
Description
Note
Example 1
Example 2
Solution 1(C++)
class Solution { public: int findNthDigit(int n) { // step 1. calculate how many digits the number has. long base = 9, digits = 1; while (n - base * digits > 0) { n -= base * digits; base *= 10; digits ++; } // step 2. calculate what the number is. int index = n % digits; if (index == 0) index = digits; long num = 1; for (int i = 1; i < digits; i ++) num *= 10; num += (index == digits) ? n / digits - 1 : n / digits;; // step 3. find out which digit in the number is we wanted. for (int i = index; i < digits; i ++) num /= 10; return num % 10; } };
算法分析
这道题,挺无聊的,首先确定n是几位数。然后确定是哪一个数的第几位。最后找出该位上的数字就好了。程序分析
这个对编程要求比较高。相关文章推荐
- LeetCode—400. Nth Digit
- [Leetcode] 400. Nth Digit 解题报告
- [leetcode] 400. Nth Digit 解题报告
- [LeetCode]--400. Nth Digit
- [LeetCode]400. Nth Digit(规律)
- leetcode 400. Nth Digit
- 400. Nth Digit (leetcode) 。。。
- LeetCode 400. Nth Digit
- LeetCode 400. Nth Digit
- LeetCode 400. Nth Digit
- 【Leetcode】400. Nth Digit
- LeetCode400. Nth Digit
- Leetcode 400. Nth Digit 第n个数字 解题报告
- [LeetCode]400. Nth Digit
- LeetCode 400. Nth Digit
- [leetcode: Python]400. Nth Digit
- 【leetcode】400. Nth Digit(Python & C++)
- 【LeetCode】 400. Nth Digit
- LeetCode 400. Nth Digit
- Leetcode 400. Nth Digit[easy]