leetcode 400. Nth Digit解题报告
来源:互联网 发布:mac os镜像下载iso 编辑:程序博客网 时间:2024/06/10 19:56
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:3Output:3
Example 2:
Input:11Output: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;}
1 0
- [leetcode] 400. Nth Digit 解题报告
- leetcode 400. Nth Digit解题报告
- [Leetcode] 400. Nth Digit 解题报告
- Leetcode 400. Nth Digit 第n个数字 解题报告
- 【Leetcode】400. Nth Digit
- leetcode 400. Nth Digit
- 400. Nth Digit (leetcode) 。。。
- [LeetCode]400. Nth Digit
- [leetcode] 400. Nth Digit
- [LeetCode]--400. Nth Digit
- leetcode 400. Nth Digit
- LeetCode 400. Nth Digit
- [leetcode]400. Nth Digit
- Leetcode 400. Nth Digit
- LeetCode 400. Nth Digit
- 【LeetCode】 400. Nth Digit
- LeetCode 400. Nth Digit
- LeetCode 400. Nth Digit
- css解析原理
- JavaScript-对闭包的理解
- 线段树
- idea 15.0.2 破解
- Java面试题大全(Java基础十)
- leetcode 400. Nth Digit解题报告
- static关键字
- Android系统编写Linux内核驱动程序
- ubuntu14.04搭建Android Studio2.1开发环境activity_main.xml页面不显示虚拟手机的解决办法
- oracle入门很简单:四、本地Net服务名配置
- 2016-9-19小结
- CentOS6.5系统下安装Apache2.4+PHP5.6+Mysql5.5 (LAMP)
- 【Android】 RecyclerView、ListView实现单选列表的优雅之路.
- 模拟器运行错误: invalid device state