当前位置:网站首页>【剑指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';
}
}
边栏推荐
- [proteus simulation] H-bridge drive DC motor composed of triode + key forward and reverse control
- [observation] innovation in the software industry has entered a "new cycle". How can we make a new start in the changing situation?
- 模拟串口UART的实现
- 阿里云视频点播播放出错,控制台访问出现code:4400
- CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
- 理财产品在双休日可以赎回吗?
- Oh, my God, it's a counter attack by eight part essay
- How to calculate the Gini coefficient in R (with examples)
- Cross domain cors/options
- 直播预报|中国信息协会网信大讲堂第六期将于6月24日开播啦
猜你喜欢

AAAI 2022 | traditional Gan can be interpreted after modification, and the interpretability of convolution kernel and the authenticity of generated image are guaranteed

Using qtest for data set test performance test GUI test

讲真,Kotlin 协程的挂起没那么神秘(原理篇)

EasyDSS问题及解决方案汇总

【513. 找树左下角的值】

智能計算之神經網絡(BP)介紹

Visualization of wine datasets in R language

Three months of self-taught automatic test, salary from 4.5K to 15K, who knows what I have experienced?

R语言 co2数据集 可视化

慕课6、实现负载均衡-Ribbon
随机推荐
Raspberry pie environment settings
[proteus simulation] NE555 delay circuit
How to calculate the Gini coefficient in R (with examples)
[proteus simulation] 8x8LED dot matrix digital cyclic display
MySQL高级(二)
what? You can't be separated by wechat
Easyclick fixed status log window
Dynamicdatabasesource, which supports the master-slave database on the application side
扩展Ribbon支持Nacos权重的三种方式
Software testing - Test Case Design & detailed explanation of test classification
Kotlin1.6.20新功能Context Receivers使用技巧揭秘
Easyclick update Gallery
EasyClick更新图库
[graduation season] step by step? Thinking about four years of University by an automation er
Several common MySQL commands
Feign常见问题总结
An IPFs enabled email - skiff
Cryptography series: certificate format representation of PKI X.509
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
Oracle system/用户被锁定的解决方法