Leetcode 400.Nth Digit
題目的意思是給定一串無窮大的正整數,讓我們找出第n個數字是多少。
思路:我們可以把這些數字寫出來找下規律,每個數由一個數字構成的數有9個,每個數由2個數字構成的數有90個,每個數由3個數字構成的數有900個,每個數由n個數字構成的數有9*10^(n-1)個。我們首先要找出第n個數字落在幾位數上,然後找到具體落在哪個數上,然後找到落到這個數的第幾位上,最後鎖定是哪一個數字。每一步都需要細心耐心。
class Solution {
public:
int findNthDigit(int m) {
int digit=1,flag;
long c=1, target=0, n=m;
//求出數字n落在digit位數上
while(n>0)
{
n-=c*digit*9;
digit++;
c*=10;
}
//求出數字n是digit位數的第幾個數字
n+=(--digit)*(c/10)*9;
//求出數字n是落在哪一個數上
target=(n-1)/digit+c/10; //
//求出數字n是落在target左數第flag個數字上
flag=(n-1)%digit+1;
//求出target的右數第digit-flag+1數字
return target/(int)pow(10,digit-flag)%10;
}
};
相關文章
- LeetCode-Number of Digit OneLeetCodeGit
- Nth Digit【難】Git
- Last digit of a huge numberASTGit
- ZOJ First Digit(瞎搞)Git
- LintCode-Digit CountsGit
- Nth Digit 第N個數字Git
- 233. Number of Digit OneGit
- UVa 1225 - Digit CountingGit
- HDU 1060 Leftmost DigitGit
- Digit Counting uva1225Git
- HDU 1060 Leftmost Digit(數論,c++)GitC++
- POJ3187Backward Digit Sums[楊輝三角]Git
- BZOJ4421 : [Cerc2015] Digit DivisionGit
- 生成元(Digit Generator, AMC/ICPC Seoul 2005, UVa1583)Git
- HDU - 1061 Rightmost Digit(二分快速冪板題)Git
- PARI/GP網站上連線的一本書The SIAM 100-Digit Challenge網站Git
- oracle lz047中的REGEXP_LIKE(cust_first_name,'[[:digit:]]')) 轉OracleGit
- Martyr2專案實現——Number部分的問題求解 (1) Find Pi to Nth DigitGit
- 【LeetCode】如何學習LeetCode?LeetCode
- P1118 [USACO06FEB]數字三角形Backward Digit Su…Git
- LeetCodeLeetCode
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode
- Leetcode SortListLeetCode
- Leetcode ReorderListLeetCode
- leetcode SubsetsLeetCode
- Leetcode PermutationsLeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- 我的 LeetCodeLeetCode
- Leetcode學習LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- LeetCode 克隆圖LeetCode
- Leetcode Perfect SquaresLeetCode
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- 【LeetCode】Jewels and StonesLeetCode
- 嘗試 LeetcodeLeetCode