C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit
在线提交网址: https://leetcode.com/problems/nth-digit/
- Total Accepted: 4356
- Total Submissions: 14245
- Difficulty: Easy
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.
Tags: Math
分析:
1.1位数共有9=9·1个, 1~9;
2.2位数共有90=9·10个, 10~99;
3.3位数共有900=9·10·10个, 100~999;
…
已AC代码:
#include<cstdio>
#include<iostream>
using namespace std;
class Solution {
public:
int findNthDigit(int n) {
int len = 1, base = 1; // len表示当前数的位数, base表示当前位是个位、百位、千位等...
while (n > 9L * base * len) {
n -= 9 * base * len;
len++;
base *= 10;
}
int curNum = (n - 1)/len + base, digit = 0; // curNum是含有所找digit的那个数
for (int i = (n - 1) % len; i < len; ++i) { // 根据偏移量找到所找的数字
digit = curNum % 10;
curNum /= 10;
}
return digit;
}
};
// 以下为测试
int main() {
Solution sol;
int n;
cin>>n; // 150
int res = sol.findNthDigit(n);
cout<<res<<" "<<endl;
return 0;
}
C++版 - Leetcode 400. Nth Digit解题报告的更多相关文章
- 【LeetCode】400. Nth Digit 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- [leetcode] 400. Nth Digit
https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- HTML5+CSS3 1
html5标准模板 <!DOCTYPE html> //<!DOCTYPE>标签 向浏览器声明当前文档使用的HTML版本,<!DOCTYPE html>适用于所 ...
- mysql 分库分表转
分表是分散数据库压力的好方法. 分表,最直白的意思,就是将一个表结构分为多个表,然后,可以再同一个库里,也可以放到不同的库. 当然,首先要知道什么情况下,才需要分表.个人觉得单表记录条数达到百万到千万 ...
- dotnetcore http服务器研究(二)性能分析
Asp.net core kestrel 服务器性能分析 因近来发现neocli 使用asp.net core kestrel 服务器提供rpc调用,性能比较低. 和以前做过测试差异比较大,故而再次测 ...
- Exp2 后门原理与实践 20164302 王一帆
1 实验内容 1.1实验主要内容 (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, 任务计划启动 (0.5分) (3)使用MSF ...
- 安装JAVA jdk
下载软件包,jdk-10.0.1_windows-x64_bin 根据提示一步步安装.安装完成之后,配置环境. 控制面板——>系统——>编辑系统变量 4. 系统变量 增加path变 ...
- centos7系统下配置jdk环境变量
用到的工具, Xshell5, Xftp5 现在的jdk7下载链接不好找, 打开下载界面, 全是jdk8的, 我这里提供了一份百度云的下载地址http://pan.baidu.com/s/1jHF9m ...
- js判断时间段
开始时间小于结束时间的判断,下面是封装号的方法,直接可以调用: var data = new Date(); var year = data .getFullYear(); //获取完整的年份(4位) ...
- linux查看分区是否开启acl权限
1.为什么需要ACL权限 ACL的全称是 Access Control List (访问控制列表) .对于文件或者目录,都有相应的操作权限 r(read 读),w(write 写),x(execute ...
- 用java从0生成一个简单的excel
用java从0生成一个简单的excel 目标 用代码实现对一个excel的基础操作,包括创建,插入文字,(好像就这些了),生成的excel可以用wps打开,如果直接用c++的文件流会生成假的xls表格 ...
- tmux使用中出现的问题和解决方式
常用操作: tmux ls 看当前都有哪些sessiontmux new -s my1 创建窗口,名为my1ctrl+B,D 退出窗口 (这个就是同时按ctrl和B,然后松开后再按D键)tmux at ...