当前位置:网站首页>【剑指Offer】面试题44.数字序列中的某一位数字
【剑指Offer】面试题44.数字序列中的某一位数字
2022-06-22 19:29:00 【LuZhouShiLi】
面试题44.数字序列中的某一位数字
题目
数字以0123456789101112131415…的格式序列化到一个字符序列中。在这个序列中,第5位(从下标0开始计数)是5,第13位是1,第19位是4,等等。
请写一个函数,求任意第n位对应的数字
思路
找规律,真的会谢
https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/mian-shi-ti-44-shu-zi-xu-lie-zhong-mou-yi-wei-de-6/
代码
class Solution {
public int findNthDigit(int n) {
int digit = 1;
long start = 1;
long count = 9;
while(n > count)
{
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
// 确定n所在的数字
long num = start + (n - 1) / digit;
// 确定N 在num的哪一个数位
return Long.toString(num).charAt((n - 1) % digit) - '0';
}
}
边栏推荐
- JWT简介
- R language usarrests dataset visualization
- A Dynamic Near-Optimal Algorithm for Online Linear Programming
- 模拟串口UART的实现
- Container container runtime (2): which is better for you, yum installation or binary installation?
- The road to systematic construction of geek planet business monitoring and alarm system
- [graduation season] step by step? Thinking about four years of University by an automation er
- 已解决:一个表中可以有多个自增列吗
- 84-我对网传<52 条 SQL 语句性能优化策略>的一些看法
- 怎样实现网页端im即时通讯中的@人功能
猜你喜欢
![[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.](/img/da/9ced1c0a9c386bc8da75dddaa443e5.png)
[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.

【513. 找树左下角的值】

Emotion analysis with RNN & CNN pytorch

智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍

迅睿CMS 自定义数据接口-php执行文件代码

Three dimensional world helps the laboratory to consolidate the complete quality system management

Easydss problem and solution summary

CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务

Ribbon load balancing

天,靠八股文逆袭了啊
随机推荐
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
模拟串口UART的实现
80-分页查询,不止写法
EasyClick更新图库
How to calculate yoy and mom in MySQL
慕课5、服务发现-Nacos
[observation] innovation in the software industry has entered a "new cycle". How can we make a new start in the changing situation?
One picture decoding opencloudos community open day
迅睿CMS 自定义数据接口-php执行文件代码
Introduction of Neural Network (BP) in Intelligent Computing
Kotlin1.6.20 new features context receivers tips
Feign常见问题总结
89-oracle SQL写法与优化器缺陷一例
扩展Ribbon支持Nacos权重的三种方式
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
评估指标及代码实现(NDCG)
智能计算之神经网络(BP)介绍
深度学习常用损失函数总览:基本形式、原理、特点
R language CO2 dataset visualization
R 语言USArrests 数据集可视化