当前位置:网站首页>剑指offer 44【数字序列中的某一位】【100%,100%】
剑指offer 44【数字序列中的某一位】【100%,100%】
2022-07-16 19:26:00 【星光技术人】
剑指offer 44【数字序列中的某一位】

方法1:纯数学解法
纯数学解题思路
四个函数
- findNthDigit()
- get_i_num(int n,int i) 返回数字n从前往后数的第i个数字,123的第二个数字为2
- pow(int n, int i) 求n的i次方
- get_count(int i) 将不同长度的数字分组,最高位时个位的数字一共有9个(1-9);
最高位为十位的数字一共有90个(10-99),连接形成90*2个位数
依次类推
首先计算我们想要的结果是落在那批数中
然后计算结果落在那个数字中
然后计算结果时目标数字的第几位数
class Solution {
public:
int findNthDigit(int n) {
if (n == 0)
return 0;
long long sum = 0;
long long rest = n; //当前指向往前数几个
int i = 0; //表示长度为i的数字
while (n > sum)
{
i++;
sum = sum + get_count(i); //现在指针指向第几各数字
rest -= get_count(i);
}
rest += get_count(i);
int nn = rest / i + pow(10, i - 1); //我们要求得结果是从长度为i得数字中拿出来
int mm = rest % i;
if (mm == 0)
return get_i_num(nn-1, i);
return get_i_num(nn, mm);
}
//返回数n的第i个数
int get_i_num(int n, int idx)
{
int L = 0;//数n是几位数
int temp = 1;
while (n / temp != 0)
{
L++;
temp *= 10;
}
int res = n % pow(10, L - idx + 1) / pow(10, L - idx);
return res;
}
long long pow(int n, int i)
{
long long x = n;
long long res = 1;
while (i)
{
if (i & 1)
res *= x;
x = x * x;
i = i >> 1;
}
return res;
}
long long get_count(int i)
{
return 9 * pow(10, i - 1) * i;
}
};
- 解法2
class Solution {
public:
int findNthDigit(int n) {
if(n==0)
return 0;
int i = 1;
long long start = 1;
long long count = 9;
while(n>count)
{
n -= count;
i ++;
start = start*10;
count = 9*start*i;
}
int m = start + (n-1)/i;
return to_string(m)[(n-1)%i] - '0';
}
};
边栏推荐
- ES6-新增的数组方法之最常用的几种 map(),filter(),reduce(),forEach(),
- Eight mainstream OA office software competition, traditional vs. rookie, who do you pick?
- 博客从 CloudBase 迁移至云主机
- 是时候聊聊RPA了
- 网络基础VlAN配置(eNSP、Cisco)
- 低代码搭建医疗企业数字化CRM案例分析
- CONDA create delete environment
- Case study on how low code helps the new growth of non-standard retail business
- 信息收集
- MySQL variables, process control and cursors
猜你喜欢

Win11怎么打开3D查看器

华为和荣耀手机升级鸿蒙系统之后与matebook无法多屏协同的问题
![(note sorting is not completed) [graph theory: minimum spanning tree]](/img/c4/0868577ebc32a027a49735f4417421.png)
(note sorting is not completed) [graph theory: minimum spanning tree]

1302_FreeRTOS中CoRoutine设计实现分析

What are the directions of voice signal processing in audio and video?

什么样的无线蓝牙耳机好?综合性能最好的蓝牙耳机

Inventory of major RPA products at home and abroad

Leetcode 1332. 删除回文子序列(看完题解才恍然大悟!!!!!!!)

AcWing 396. 矿场搭建 题解(tarjan割点)

Leetcode 1309. 解码字母到整数映射(可以,一次过)
随机推荐
Huawei and glory mobile phones cannot cooperate with matebook on multiple screens after upgrading the Hongmeng system
The 13th update of go project [ten years of open source] (the "corpse fraud" two months after the change)
I rolled up a small and beautiful [markdown static blog program] at home
CONDA installation requirements Txt specified dependent package
Win11怎么打开3D查看器
Best practices for exclusive resource pool use -notebook and training task linkage
JS export JSON array to excel
是时候聊聊RPA了
【图文并茂】U盘启动盘制作 U盘启动盘重装系统教程
阿普奇 ABOX-700 工控机 MinipiceCAN卡在电力巡检机器人中的应用
[Huang ah code] Why do I suggest you choose go instead of PHP?
On learning relational operators in JS
LeetCode 第23天
【微信小程序】progress(93/100)
Pyqt5 file dialog (QFileDialog)
一人用低代码开发平台搭建整个集团的数字化系统解决方案
Phabricator Conduit API介绍
Best practices for the use of exclusive resource pools - Introduction to the use of dependent services
MySQL到底是如何执行SQL语句的
通感一体化系统的下行功率分配技术