400. Nth Digit (找第n个数字)
2016-10-28 11:39
239 查看
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:
Example 2:
public class Solution {
public int findNthDigit(int n) {
int len = 1;
long num = 9, start = 0;
while (n > num*len) {
n -= len * num;
start+=num;
len++;
num *= 10;
}
int res;
if(n%len==0){
res = (""+(start+n/len)).charAt(len-1)-'0';
}else{
res = (""+(start+n/len+1)).charAt(n%len-1)-'0';
}
return res;
}
}
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
Example 1:
Input: 3 Output: 3
Example 2:
Input: 11 Output: 0 Explanation: 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.
public class Solution {
public int findNthDigit(int n) {
int len = 1;
long num = 9, start = 0;
while (n > num*len) {
n -= len * num;
start+=num;
len++;
num *= 10;
}
int res;
if(n%len==0){
res = (""+(start+n/len)).charAt(len-1)-'0';
}else{
res = (""+(start+n/len+1)).charAt(n%len-1)-'0';
}
return res;
}
}
相关文章推荐
- leetcode_400. Nth Digit 查找自然数序列中的第n个数字
- Leetcode 400. Nth Digit 第n个数字 解题报告
- (hdu step 4.1.5)find the nth digit(求S串中的第n个位置上是什么数字)
- leetcode 400. Nth Digit 从1到n第n位数字是什么 + 找规律
- 【BZOJ】3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(博弈论)
- 深度学习之OCR(一)——多数字识别_Multi-digit Number Recognition
- 400. Nth Digit
- bzoj 3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(SG函数)
- [USACO06FEB]数字三角形Backward Digit Su…
- leetcode 233. Number of Digit One 从1到n的数组中出现数字1的数量 + 寻找规律,公式计算
- 400. Nth Digit
- 习题3-3 数数字(Digit counting)
- [LeetCode]400. Nth Digit
- UVa 1225 Digit Counting(数字统计)
- Uva1225-Digit Counting-数数字
- 400. Nth Digit
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
- GDOI2012 数字统计(digit) 简单的公式推导(高精度模板题)
- leetcode 400. Nth Digit
- Kaggle Digit Recognizer 基于sklearn实现的手写数字识别 for MNIST data