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 ...
随机推荐
- mysql 标点符号
w攻防一体化.
- 并发编程 - 线程 - 1.互斥锁/2.GIL解释器锁/3.死锁与递归锁/4.信号量/5.Event事件/6.定时器
1.互斥锁: 原理:将并行变成串行 精髓:局部串行,只针对共享数据修改 保护不同的数据就应该用不用的锁 from threading import Thread, Lock import time n ...
- git学习——<二>git配置文件
一.git所有配置文件 <一>./etc/gitconfig全局配置文件 修改该配置文件,会对所有用户有影响. 使用git config --system来配置该文件 <二>. ...
- 浅谈virtualenv(虚拟环境)
简介 virtualenv为应用提供了隔离的Python运行环境,解决了不同应用间多版本的冲突问题. 例如: 如果我们要同时开发多个应用程序,那这些应用程序都会共用一个Python,就是安装在系统的P ...
- Linux cp命令
cp命令(copy),用来对一个或多个文件,目录进行拷贝 1.语法 cp [选项] [参数] 2.命令选项 -b 当文件存在时,覆盖前,为其创建一个备份-d 当复制软连接时,把目标文件或目录也建立为软 ...
- 009-Hadoop Hive sql语法详解4-DQL 操作:数据查询SQL-select、join、union、udtf
一.基本的Select 操作 语法SELECT [ALL | DISTINCT] select_expr, select_expr, ...FROM table_reference[WHERE whe ...
- sipp模拟freeswitch分机测试(SIP协议调试)
1.freeswitch安装 1) 网上很多安装方法都不靠谱,系统版本,各种依赖库一堆问题,下面是验证的可行的. yum install -y http://files.freeswitch.org/ ...
- Loadrunner中参数化取值方式分析
Loadrunner中参数化取值依赖两个维度: 1.取值顺序分为“顺序”“随机”“唯一”. select next row:Sequential , Random,unique 2.更新值时分为 ...
- 【转帖】C++经典书籍汇总
TCPL和D&E 1:Bjarne Stroustrup, The C++ Programming Language (Special 3rd Edition) <C++ 程序设计语言( ...
- 10、property
成人的BMI数值:过轻:低于18.5正常:18.5-23.9过重:24-27肥胖:28-32非常肥胖, 高于32 体质指数(BMI)=体重(kg)÷身高^2(m) EX:70kg÷(1.75×1.75 ...