400. Nth Digit
来源:互联网 发布:js怎么获取对象的长度 编辑:程序博客网 时间:2024/04/20 01:10
题目:
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
Example 1:
Input:3Output:3
Example 2:
Input:11Output:0Explanation:The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.思路:
本题题意比较难以理解,看了官方的solution才发现了题目的含义:寻找n是整数序列中的第几个数字(digit),是数字所以1-10,一共有11个数字,这样理解了题目的目的后,解题思路就有了,参考主流方法,分为三步:
step1:找到n落在哪个位数区间1-9;10-99;100-999;.....
step2:找到n落在哪个数值上
step3:找到n落在哪个位上
代码:
class Solution {public: int findNthDigit(int n) { long digit = 1, ith = 1, base = 9; while(n > base*digit) { n -= base*(digit++); ith += base; base *= 10; } return to_string(ith+(n-1)/digit)[(n-1)%digit]-'0'; }};
阅读全文
1 0
- 【Leetcode】400. Nth Digit
- 400. Nth Digit
- leetcode 400. Nth Digit
- 400. Nth Digit
- 400. Nth Digit
- 400. Nth Digit (leetcode) 。。。
- [LeetCode]400. Nth Digit
- [leetcode] 400. Nth Digit
- 400. Nth Digit
- [LeetCode]--400. Nth Digit
- 400. Nth Digit
- leetcode 400. Nth Digit
- LeetCode 400. Nth Digit
- 400. Nth Digit
- 400. Nth Digit
- [leetcode]400. Nth Digit
- Leetcode 400. Nth Digit
- 400. Nth Digit
- 浅谈hibernate的sessionFactory和session
- 逆向实战之计算管家会员破解
- 20170809工作记账流水(2017-08-09 10:00)
- 每日一题——排序(从1-n连续的数进行排序)
- Coding心得
- 400. Nth Digit
- 【codevs 1557】热浪(SPFA)
- 深度学习网络搭建与编程范式
- 因为方言问题使得hibernate在eclipse上建表失败
- HashMap常见面试问题总结
- 关于JAVA接口
- 覆盖和多态的条件
- git操作
- Linux的特点