当前位置:网站首页>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;
}
}
边栏推荐
- 关于shell脚本的一些思考
- 收藏-即时通讯(IM)开源项目OpenIM-功能手册
- Cesium 修改鼠标样式
- 解决This application failed to start because no Qt platform plugin could be initialized的办法
- 业界新标杆!阿里开源自研高并发编程核心笔记(2022 最新版)
- 检测和控制影子IT的五个步骤
- LeetCode_位数统计_中等_400.第 N 位数字
- TweenMax.js向日葵表情变化
- tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)
- 若依集成easyexcel实现excel表格增强
猜你喜欢

leetcode 231. Powers of 2

15 years experience in software architect summary: in the field of ML, tread beginners, five hole

后台图库上传功能

转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G

DDD 中的几个困难问题

Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design

YARN功能介绍、交互流程及调度策略

敏捷交付的工程效能治理

在树莓派上搭建属于自己的网页(3)

TweenMax.js向日葵表情变化
随机推荐
抖音web逆向教程
信使mRNA甲基化偶联3-甲基胞嘧啶(m3C)|mRNA-m3C
leetcode 072. 求平方根
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
华为设备配置VRRP与BFD联动实现快速切换
华为设备VRRP配置命令
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
leetcode 326. Powers of 3
敏捷交付的工程效能治理
AWTK开发编译环境踩坑记录1(编译提示powershell.exe出错)
直播小程序源码,UI自动化中获取登录验证码
通关剑指 Offer——剑指 Offer II 009. 乘积小于 K 的子数组
回忆三年浮沉
xss.haozi练习通关详解
收藏-即时通讯(IM)开源项目OpenIM-功能手册
Leetcode sword refers to Offer 15. 1 in the binary number
后台图库上传功能
9月1日起我国给予多哥等16国98%税目产品零关税待遇
chartjs自定义柱状图插件
2022年1~7月语音合成(TTS)和语音识别(ASR)论文月报