当前位置:网站首页>【剑指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';
}
}
边栏推荐
- what? You can't be separated by wechat
- Golang学习笔记—结构体
- 用RNN & CNN进行情感分析 - PyTorch
- Code to Image Converter | 代码生成漂亮图片工具
- 扩展Ribbon支持Nacos权重的三种方式
- The real king of cache
- From perceptron to transformer, a brief history of deep learning
- 真正的缓存之王Caffine Cache
- Alibaba cloud video on demand playback error, console access code:4400
- Overview of common loss functions for in-depth learning: basic forms, principles and characteristics
猜你喜欢

Container container runtime (2): which is better for you, yum installation or binary installation?

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

Ribbon负载均衡

Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)

字节跳动提出轻量级高效新型网络MoCoViT,在分类、检测等CV任务上性能优于GhostNet、MobileNetV3!...
![Web technology sharing | [Gaode map] to realize customized track playback](/img/80/7daba6716b85276de8d09b9d016313.png)
Web technology sharing | [Gaode map] to realize customized track playback

From perceptron to transformer, a brief history of deep learning

Visualization of R language penguins dataset

MySQL Basics - functions

Teach you how to create SSM project structure in idea
随机推荐
Multi transactions in redis
mysql8.0忘记密码的详细解决方法
Alibaba cloud video on demand playback error, console access code:4400
底部菜单添加的链接无法跳转到二级页面的问题
Easyclick fixed status log window
89-oracle SQL写法与优化器缺陷一例
R语言penguins数据集可视化
uniapp小程序商城开发thinkphp6积分商城、团购、秒杀 封装APP
[proteus simulation] NE555 delay circuit
MySQL Basics - functions
MySQL foundation - constraints
慕课6、实现负载均衡-Ribbon
Easydss problem and solution summary
CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
已解决:一個錶中可以有多個自增列嗎
Three dimensional world helps the laboratory to consolidate the complete quality system management
密码学系列之:PKI的证书格式表示X.509
Kotlin1.6.20 new features context receivers tips
91-oracle普通表改分区表的几种方法
Ribbon负载均衡