Leetcode: Nth Digit
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:
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.
1-9 : count:9 * len:1
10-99: count:90 * len:2
100-999: count:900 * len:3
1000-9999: count: 9000 * len:4
maintain a count, len, start
public class Solution {
public int findNthDigit(int n) {
int start = 1;
int len = 1;
long count = 9;
while (n > len*count) {
n -= len*count;
start *= 10;
len ++;
count *= 10;
}
start += (n-1)/len;
char res = Integer.toString(start).charAt((n-1)%len);
return Character.getNumericValue(res);
}
}
Leetcode: Nth Digit的更多相关文章
- [LeetCode] Nth Digit 第N位
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- LeetCode——Nth Digit
Question Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... ...
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- 【LeetCode】400. Nth Digit 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Nth Digit | leetcode
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- leetcode 400 Add to List 400. Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...
- [Swift]LeetCode400. 第N个数字 | Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...
- hdu 1597 find the nth digit
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- find the nth digit(二分查找)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597 find the nth digit Time Limit: 1000/1000 MS (Jav ...
随机推荐
- FileResult,JavaScriptResult,JsonResult
FileResult:可以响应任意文档的属性,包括二进制格式的数据,eg:图档,pdf,excel,zip,可以传入byte[],文档路径,Stream等不同的属性,让mvc将属性回传给客户端,除此之 ...
- 基于LR的Oracle应用性能测试
最近对一个oracle ERP系统的INV模块进行性能测试,因为之前大部分都是测试web类型的应用,在这方面经验较少,期间也遇到了不少问题,因此有必要作些总结,以备后忘.首先先简单了解下测试对象相关的 ...
- the core or essence of a computer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...
- Strong AI Versus Weak AI
Computer Science An Overview _J. Glenn Brookshear _11th Edition The conjecture that machines can be ...
- P1970 花匠
状态定义是dp中非常重要的,可以直接影响到效率,如此题,第一种思路是: #include <bits/stdc++.h> using namespace std; const int ma ...
- android硬件调试之无法识别android设备解决办法
DDMS 中无法识别华为荣耀六手机, 用豌豆荚开始显示无法连接, 用豌豆荚安装完驱动后,就可以连接了 http://www.zhihu.com/question/30588024 http://w ...
- JS使用ActiveXObject读取数据库代码示例(只支持IE)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 在 mac os 上搭建 git server
前言:之前学习了如何使用 git 后,一直想搭建一个本机搭建一个 git server 的,一开始不知道走了弯路用了 gitosis,折腾了我好几天都没配置好.昨晚查资料发现 gitosis 早就过时 ...
- 编写category时的便利宏(用于解决category方法从静态库中加载需要特别设置的问题)
代码摘录自YYKit:https://github.com/ibireme/YYKit /** Add this macro before each category implementation, ...
- 安装mssql2008和启动时出现的问题及解决办法
(一) 安装sql server 2008 时, 提示错误:此计算机上安装了 Microsoft Visual Studio 2008 的早期版本. 请在安装 SQL Server 2008 前将 V ...