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 ...
随机推荐
- MyISAM和InnoDB索引实现对比
MyISAM索引实现 MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.如图: 这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索引 ...
- MyBatis 3 分页
利用MyBatis 3进行分页,选定的数据库表c_province,有3列,id列,provinceid列,province列,用Oracle数据库.首先建立一个对应的实体类,Province有3个属 ...
- Leetcode 221.最大的正方形
最大的正方形 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出 ...
- 【CSS】position relative 用法
Relative是position的一个属性,是相对定位. position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:stat ...
- 【课余作品】简约QQ申请器(永久免费更新)
UPDATEDATE:2013-08-28 下载地址:http://pan.baidu.com/share/link?shareid=3376000151&uk=3339155803
- 使用jemalloc优化nginx和mysql内存管理
预先安装autoconf 和 make yum -y install autoconf make jemalloc的安装jiemalloc 开源项目网站 http://www.canonware.co ...
- 在mac下安装matplotlib,xlrd
brew install freetype brew install libpng sudo easy_install pip#图形显示模块 sudo pip install matplotlib 输 ...
- X230 安装 EI Capitan 10.11.5 总结
/* 写这个文章的目的主要是为了帮助我自己理清思路,如果能顺便帮助到您.even better */ 在动手之前大致浏览了 远景论坛(国内第一黑苹果社区)置顶帖的全部内容 [新人请看]远景 ...
- github新建本地仓库,再同步远程仓库基本用法
github新建本地仓库,再同步远程仓库基本用法 1 mkdir gitRepo 2 cd gitRepo 3 git init #初始化本地仓库 4 git add xxx #添加要push到远 ...
- SQL SERVER 2012 第五章 创建和修改数据表 の CREATE语句
CREATE <object type> <object name> CREATE DATABASE <database name> 比较完整的语法列表: 日志文件 ...