leetcode 400. Nth Digit

在线提交网址: https://leetcode.com/problems/nth-digit/

  • Total Accepted: 4356
  • Total Submissions: 14245
  • Difficulty: Easy

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …

Note:

n is positive and will fit within the range of a 32-bit signed integer (n < 231).

Example 1:

Input:
3 Output:
3

Example 2:

Input:
11 Output:
0

Explanation:

The 11th digit of the sequence `1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...` is a 0, which is part of the number 10.

Tags: Math

分析:

1.1位数共有9=9·1个, 1~9;

2.2位数共有90=9·10个, 10~99;

3.3位数共有900=9·10·10个, 100~999;

已AC代码:

#include<cstdio>
#include<iostream>
using namespace std; class Solution {
public:
int findNthDigit(int n) {
int len = 1, base = 1; // len表示当前数的位数, base表示当前位是个位、百位、千位等...
while (n > 9L * base * len) {
n -= 9 * base * len;
len++;
base *= 10;
}
int curNum = (n - 1)/len + base, digit = 0; // curNum是含有所找digit的那个数
for (int i = (n - 1) % len; i < len; ++i) { // 根据偏移量找到所找的数字
digit = curNum % 10;
curNum /= 10;
}
return digit;
}
};
// 以下为测试
int main() {
Solution sol;
int n;
cin>>n; // 150
int res = sol.findNthDigit(n);
cout<<res<<" "<<endl;
return 0;
}

C++版 - Leetcode 400. Nth Digit解题报告的更多相关文章

  1. 【LeetCode】400. Nth Digit 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  3. [leetcode] 400. Nth Digit

    https://leetcode.com/contest/5/problems/nth-digit/ 刚开始看不懂题意,后来才理解是这个序列连起来的,看一下第几位是几.然后就是数,1位数几个,2位数几 ...

  4. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. 修改或添加HTTP请求头

    1.Cookie的修改 (一)cookie长什么样 cookie是一个个键值对(“键=值”的形式)加上分号空格隔开组合而成, 形如: "name1=value1; name2=value2; ...

  2. (转载)Linux终端复用神器-Tmux使用

    Linux终端复用神器-Tmux使用 转载地址:https://blog.51cto.com/652465/2094738 Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于Open ...

  3. 字节跳动19春招研发第一次在线笔试-A卷

    结果:凉. 说明 此博客仅为笔试记录所用,如涉及版权或保密问题,请联系我及时删除. 联系方式:georgehu716@qq.com 1. 找零 时间限制:C/C++ 1秒,其他语言 2秒 空间限制:C ...

  4. Win10家庭版WindowsUpdate属性为灰色

    一般的取消Windows更新只需要打开任务管理器,点击服务 然后点击左下角的打开服务 找到WindowsUpdate,右键属性 按照正常的电脑只要在启动类型中选择禁用,然后在恢复里的第一次操作选择无操 ...

  5. toString

    在java中使用toString: 如果在Java在输出定义一个Person类 然后实例化person  per 直接用system.out.println(per);无法得到我们想要的实例化内容 p ...

  6. 图解CSS3核心技术与案例实战(1)

    前言: 我买了一本<图解CSS3核心技术与案例实战>大漠写的,为了提高自己的自觉性呢,抓紧看书,把读书笔记放在这上面,跟大家一起分享,也为督促自己完成读书计划. 文末有微信公众号,感谢你的 ...

  7. sqlzoo:5

    展示世界的總人口. SELECT sum(population) FROM world 列出所有的洲份, 每個只有一次. select distinct(continent) from world 找 ...

  8. Ubuntu全盘备份与恢复,亲自总结,实测可靠

    https://blog.csdn.net/sinat_27554409/article/details/78227496 Ubuntu全盘备份与恢复,亲自总结,实测可靠 初学者在使用Ubuntu这类 ...

  9. 二维数组的最大子数组和 时间复杂度:O(n的四次方)

    先上代码 小组成员:高达,李奔 package 三月二十一号; import java.io.BufferedReader; import java.io.FileReader; import jav ...

  10. 微信小程序之canvas绘制海报分享到朋友圈

    绘制canvas内容 首先,需要写一个canvas标签,给canvas-id命名为shareBox <canvas canvas-id="shareBox"></ ...