当前位置:网站首页>【剑指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';
}
}
边栏推荐
- PlainSelect.getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
- 密码学系列之:PKI的证书格式表示X.509
- MySQL advanced (II)
- R语言AirPassengers数据集可视化
- 天,靠八股文逆袭了啊
- Golang學習筆記—結構體
- Emotion analysis with RNN & CNN pytorch
- AAAI 2022 | traditional Gan can be interpreted after modification, and the interpretability of convolution kernel and the authenticity of generated image are guaranteed
- [in depth understanding of tcapulusdb technology] form creation and approval of document acceptance
- 模拟串口UART的实现
猜你喜欢

How to consider the arrangement of complete knapsack

Visualization of R language nutrient dataset

Visualization of wine datasets in R language
Gradle Build Cache引发的Task缓存编译问题

Resolved: can there be multiple auto incrementing columns in a table

R语言midwest数据集可视化

Raspberry pie environment settings

Introduction to async profiler
![[observation] innovation in the software industry has entered a](/img/b8/232ce6bc41a4154f6c9d48b0819d8f.png)
[observation] innovation in the software industry has entered a "new cycle". How can we make a new start in the changing situation?
mysql8.0忘记密码的详细解决方法
随机推荐
LORA技术---LoRa信号从数据流变为LoRa扩频信号,再从射频信号通过解调变为数据
慕课6、实现负载均衡-Ribbon
Kotlin1.6.20新功能Context Receivers使用技巧揭秘
Golang學習筆記—結構體
评估指标及代码实现(NDCG)
【513. 找树左下角的值】
Easyclick fixed status log window
An IPFs enabled email - skiff
R 语言 UniversalBank.csv“ 数据分析
极客星球 | 业务监控及告警系统体系化建设之路
如何计算 R 中的基尼系数(附示例)
真正的缓存之王Caffine Cache
农产品期货开户
Web technology sharing | [Gaode map] to realize customized track playback
How to realize @ person function in IM instant messaging
what? You can't be separated by wechat
直播预报|中国信息协会网信大讲堂第六期将于6月24日开播啦
Introduction to async profiler
深度学习常用损失函数总览:基本形式、原理、特点
A detailed solution to mysql8.0 forgetting password