leetcode 400 Add to List 400. Nth Digit
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.
这道题目我之前见过。。。。一开始用下边注释的代码写,也通过了不过有点费时。后来改成取模。
class Solution {
public:
int findNthDigit(int n) {
// 2: 90 3:900 4: 9000 5:90000 6: 900000 7:9000000 8:90000000 9:900000000 10:9000000000
long x = ;
long t = ;
if (n < ) return n;
while (n > x * t) {
n -= x * t;
x++;
t *= ;
}
long sum = ;
int m = n % x;
if (m == ) return ((long)pow(10.0,x-) + n/x - )%;
else {
long q = (pow(10.0, x-) + (n/x) );
for (int j = ; j < x - m; ++j) q/=;
return q%;
}
/*
for (int i = 0; i < t; ++i) {
sum += x;
if (sum == n) {
return (i % 10);
}
else if (sum > n) {
long y = sum - n;
long z = pow(10.0,(x - 1)) + i;
cout <<z;
for (int j = 0; j < y; ++j) {
z /= 10;
}
return z%10;
}
}
*/
}
};
leetcode 400 Add to List 400. Nth Digit的更多相关文章
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- 【LeetCode】400. Nth Digit 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Nth Digit 第N位
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- Leetcode: Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- Nth Digit | leetcode
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- LeetCode——Nth Digit
Question Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [Swift]LeetCode400. 第N个数字 | Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...
随机推荐
- 【UVA 1636】 (条件概率)
题链:https://cn.vjudge.net/problem/UVA-1636 题意 给出一把枪的子弹序列,已知开了一枪且无子弹,问下一步的决策是旋转,还是开下一枪 题解 已知开一枪没子弹,下一枪 ...
- SpringMVC的删除功能
Dao层 package net.roseindia.dao; import java.util.Date; import java.util.List; import net.roseindia.m ...
- Sed命令基础操作
sed用法的小技巧 (1)在查找范围时不需要用到替换,所以不用s; (2)当只需要打印被修改行时,可以使用-n 和 –p 选项,注意二者一定配合使用: 3种方式指定命令行上的多重指令 (1)用逗号分隔 ...
- 图的最小生成树——Prim算法
Prim算法 Prim算法求最小生成树是采取蓝白点的思想,白点代表已经加入最小生成树的点,蓝点表示未加入最小生成树的点. 进行n次循环,每次循环把一个蓝点变为白点,该蓝点应该是与白点相连的最小边权的是 ...
- 1010. Radix (25)(出错较多待改进)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- [luoguP1198][JSOI2008] 最大数(线段树 || 单调栈)
题目传送门 1.线段树 线段树可以搞. 不过慢的要死1300+ms #include <cstdio> #include <iostream> using namespace ...
- [HDU4348]To the moon(主席树)
传送门 对于这个题,显然要打lazy标记了,但是lazy标记pushdown的时候肯定会增加一大堆节点,然后就MLE了.(题解这么说的,我其实不会pushdown) 所以,就换另一种方式,把标记直接打 ...
- idea添加虚拟参数
1. 2.
- Codeforces 660C Hard Process【二分 Or 尺取】
题目链接: http://codeforces.com/problemset/problem/660/C 题意: 给定0.1组成的数组,可以改变k个0使其为1,问最终可以得到的连续的1的最大长度. 分 ...
- 一个开发的Linux使用心得总结
Liunx介绍 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协 ...