LeetCode——Nth Digit
Question
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
Solution
就是统计位数,然后注意越界的问题,所以应该用long.
Code
class Solution {
public:
int findNthDigit(int n) {
if (n < 10)
return n;
int i = 1;
long pre = 0;
while (1) {
long value = i * 9 * pow(10, i - 1);
if (n > value) {
pre += value;
i++;
}
else {
break;
}
}
long remain = n - pre;
long y = remain / i;
long z = remain % i;
i--;
long start;
if (z > 0)
start = pow(10, i) + y;
else
start = pow(10, i) + y - 1;
stringstream ss;
ss << start;
string str;
ss >> str;
if (z > 0)
return (str[z - 1]) - 48;
else
return (str[str.length() - 1]) - 48;
}
};
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
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- 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 ...
随机推荐
- linux文件与目录管理命令(ubuntu)
ls:列出目录 选项与参数: -a:全部文件,隐藏档(开头为.的文件)也会列出: -d:仅列出目录本身(也就是 . ),而不是目录下的所有文件及目录: -l:长字符串列出,包括文件的属性.权限等数据.
- TuShare获取K线数据
Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工 到 数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据,为他们在数据获取 ...
- TypeError: cannot use a string pattern on a bytes-like object的解决办法
#!/usr/python3 import re import urllib.request def gethtml(url): page=urllib.request.urlopen(url) ht ...
- luarocks安装以及lfs安装
一.先安装lua: brew install lua 我本机的安装路径为:/usr/local/Cellar/lua/5.3.4_2 二.安装luarocks 下载luarocks的安装包: http ...
- 移动支付--银联,支付宝,微信(android)
在这个移动互联网快速发展的时代,手机已经成为人们生活或者出行之中必不可少的设备了,如今非常多城市的商户都能够採用支付宝,微信支付了.人们出门仅仅须要随身携带带手机.不用带大量现金就能够放心购物了.如今 ...
- SDUT3143:Combinatorial mathematics(组合数学)
题意:传送门 题目描述 As you know, shadow95 is pretty good at maths, especially combinatorial mathematics. Now ...
- 把typora改为微软雅黑+Consolas
前言 typora是一款非常方便的书写markdown文本的编辑器.官网:https://www.typora.io/ 对于字体强迫症患者来说,不把字体改成微软雅黑+Consolas,那是相当难受.本 ...
- 《Oracle RAC性能优化》
一 RAC环境 RAC架构,2节点信息 节点1 SQL> show parameter instance NAME TYPE ...
- matplotlib绘制散点图
参考自Matplotlib Python 画图教程 (莫烦Python)(10)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...
- Windows常见宏的使用
WIN32_LEAN_AND_MEAN 1. 参考资料:https://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs. ...