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. IMCASH:2019年区块链不会风平浪静,至少还有10件事值得期待

    当我们在说2019年是值得期待的一年时,我们还是得做到有根有据.那么,2019年在区块链行业都会发生哪些引导行业风向.影响整个行业的事件呢? 今天,白话区块链带着大家顺着时间线梳理一下. 01 第一季 ...

  2. c语言第一次作业--分支 顺序结构

    1.1思维导图 1.2.1本周学习体会以及代码量学习体会 1.2.2学习体会 因为在假期时只看了小部分的学习视频,也没有刷题量,导致了在开始就感觉到差同学的进程很多.刚开始觉得老师讲课很快,在恶补了很 ...

  3. 基于centos7系统部署cobbler

    准备环境和下载cobbler 一,系统准备 虚拟机下要添加两个网卡.一个仅主机对内提供cobbler服务,一个桥接用来通外网 系统版本为:CentOS 7.5 内网ip :169.254.1.6  # ...

  4. 在NSMutableArray中添加空元素:NSNull类的使用

    有时需要将一些表示“空”的对象添加到array中.NSNull类正是基于这样的目的产生的.用NSNull表示一个占位符时,语句表达如下: [array addObject:[NSNull null]] ...

  5. Java Concurrency in Practice——读书笔记

    Thread Safety线程安全 线程安全编码的核心,就是管理对状态(state)的访问,尤其是对(共享shared.可变mutable)状态的访问. shared:指可以被多个线程访问的变量 mu ...

  6. vue中计算属性computed方法内传参

    vue中computed计算属性无法直接进行传参 如果有传参数的需求比如说做数据筛选功能可以使用闭包函数(也叫匿名函数)实现 例如: 在上篇博客vue安装使用最后的成绩表练习中的过滤功能的实现: &l ...

  7. java将数据库中查询到的数据导入到Excel表格

    1.Maven需要的依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> ...

  8. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  9. 封装一个 员工类 使用preparedStatement 查询数据 (1)

    创建员工类  自动生成get set 方法 package cn.hph; public class emp { //定义表中的属性 private int id; private String en ...

  10. f12 headers 变字典快捷方式