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 ...
随机推荐
- [SHELL]awk的用法举例
从初学awk到现在小有所成,非常感谢CUers的帮助,总结了下自己曾经遇到的问题和犯的错误,供初学者借鉴,因本人非计算机专业,对专业词汇可能有表述不对的地方,还请指正和补充! 1. awk '{cod ...
- [luoguP2420] 让我们异或吧(dfs + 异或的性质)
传送门 因为异或满足结合律和交换律. a^b^b=a 所以这个题直接求根节点到每个点路径上的异或值. 对于每组询问直接输出根到两个点的异或值的异或的值. ——代码 #include <cstdi ...
- 2016 Multi-University Training Contest 4 solutions BY FZU
1001 Another Meaning 对于这个问题,显然可以进行DP: 令dp[i]表示到i结尾的字符串可以表示的不同含义数,那么考虑两种转移: 末尾不替换含义:dp[i - 1] 末尾替换含义: ...
- CERC 2014 (动态树+主席树)
CERC 2014 Pork barrel Problem : n个点m条边有边权的无向图,有q个询问,每次询问权值在[L,R]内的边组成的最小生成树的权值和,强制在线. n <= 1000, ...
- php桥接模式
php桥接模式 桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化. 示例:当一个信息时根据发送渠道分为:QQ消息.email消息.短信消息等根据消息类型分为:普通.警告.危急等每种消息都 ...
- php统计图类库JpGraph
php统计图类库JpGraph JpGraph官网地址:https://jpgraph.net/. (1)下载类库: 下载地址:https://jpgraph.net/download/. 选择版本, ...
- jquery制作图片瀑布流点击按钮加载更多内容
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> ...
- Linux 端口开放
Linux(CentOS): 系统缺省值为32768-61000. 修改方法: 在/etc/sysctl.conf中,增加以下配置:(开放20000-50000为完成端口) net.ipv4.ip_l ...
- Distinct Subsequences (dp)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- js的基础(平民理解的执行上下文/调用堆栈/内存栈/值类型/引用类型)
与以前的切图比较,现在的前端开发对js的要求似乎越来越高,在开发中,我们不仅仅是要知道如何运用现有的框架(react/vue/ng), 而且我们对一些基础的知识的依赖越来越大. 现在我们就用平民的方法 ...