当前位置:网站首页>LeetCode_Digit Statistics_Medium_400. Nth Digit
LeetCode_Digit Statistics_Medium_400. Nth Digit
2022-08-03 20:42:00 【small town old street】
1.题目
给你一个整数 n ,请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …] 中找出并返回第 n 位上的数字.
示例 1:
输入:n = 3
输出:3
示例 2:
输入:n = 11
输出:0
解释:第 11 位数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … 里是 0 ,它是 10 的一部分.
提示:
1 <= n <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/nth-digit
2.思路
(1)位数统计
思路参考该 LeetCode 用户题解.
① 分析题目可知,We can divide this sequence of integers into an infinite number of intervals,where the length of each number in each interval(That is, it contains the number of digits)相等,这里设为 length.
length 区间 包含数字的个数
1 [1, 9] 9 * 1 = 9 * 10^0 * 1
2 [10, 99] 90 * 2 = 9 * 10^1 * 2
3 [100, 999] 900 * 3 = 9 * 10^2 * 3
4 [1000, 9999] 9000 * 4 = 9 * 10^3 * 4
... ... ...
k [10^(k - 1), 10^k - 1] 9 * 10^(length - 1) * length
... ... ...
② 根据上面的规律,I can calculate first n The length of the number to which the digit in the bit belongs length,Then calculate the first n The number to which the digit in the digit belongs(设为 num),Finally, calculate the number on the corresponding digit,The specific analysis process can be seen in the comments in the following code.
3.代码实现(Java)
//思路1————
class Solution {
public int findNthDigit(int n) {
// 设第 n The number to which the digit on the digit belongs is num,其长度 length,其初始值为 1
int length = 1;
while (9 * Math.pow(10, length - 1) * length < n) {
n -= 9 * Math.pow(10, length - 1) * length;
length++;
}
// res 保存结果
int res = 0;
// num The interval is interval = [10^(length - 1), 10^length - 1],设 start is the starting point of the interval
long start = (long) Math.pow(10, length - 1);
// 由于 interval The length of each number is equal,Therefore, the remaining n 除以 length 就等于 num 到 s 的偏移量
long num = start + n / length - 1;
// 计算 res 离 num The distance of the last digit dis
int dis = n - length * (n / length);
if (dis == 0) {
// dis 正好为 0,那么 res 就是 num 的最后一个数字,That is, the number in the one digit
res = (int) (num % 10);
} else {
res = (int) ((num + 1) / Math.pow(10, length - dis) % 10);
}
return res;
}
}
边栏推荐
- Why BI software can't handle correlation analysis
- leetcode 2119. 反转两次的数字
- TweenMax.js向日葵表情变化
- 微信小程序 生成跳转体验版url,可直接跳转到体验版小程序(可通过此方法测试模板消息)
- 详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
- Li Mu hands-on learning deep learning V2-BERT fine-tuning and code implementation
- 敏捷交付的工程效能治理
- Likou 707 - Design Linked List - Linked List
- 力扣206-反转链表——链表
- ES6--residual parameters
猜你喜欢
随机推荐
15年软件架构师经验总结:在ML领域,初学者踩过的五个坑
How can a cloud server safely use local AD/LDAP?
leetcode 461. 汉明距离
leetcode 072. 求平方根
leetcode 2119. 反转两次的数字
从开发到软件测试:除了扎实的测试基础,还有哪些必须掌握 ?
有趣的opencv-记录图片二值化和相似度实现
LeetCode 1374. 生成每种字符都是奇数个的字符串
Cesium 修改鼠标样式
leetcode 268. 丢失的数字(异或!!)
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
面试官:为什么 0.1 + 0.2 == 0.300000004?
LeetCode_位数统计_中等_400.第 N 位数字
业界新标杆!阿里开源自研高并发编程核心笔记(2022 最新版)
8.2模拟赛总结
ES6--residual parameters
尚医通项目总结
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
chartjs自定义柱状图插件
svg胶囊药样式切换按钮