[leetcode: Python]400. Nth Digit
2017-05-16 09:56
351 查看
题目:
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:
Example 2:
注意:
n是正数并且范围在32位带符号整数之内(n < 2^31)
解题思路:
将整数序列划分为下列区间:
然后分区间求值即可。
方法一:性能56ms
方法二:性能38ms
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
注意:
n是正数并且范围在32位带符号整数之内(n < 2^31)
解题思路:
将整数序列划分为下列区间:
1 1-9 2 10-99 3 100-999 4 1000-9999 5 10000-99999 6 100000-999999 7 1000000-9999999 8 10000000-99999999 9 100000000-99999999
然后分区间求值即可。
方法一:性能56ms
class Solution(object): def findNthDigit(self, n): """ :type n: int :rtype: int """ for i in range(9): d = 9 * pow(10, i) if n <= d * (i + 1): break n -= d * (i+1) n -= 1 return int(str(pow(10, i) + n/(i+1))[n % (i+1)])
方法二:性能38ms
class Solution(object): def findNthDigit(self, n): """ :type n: int :rtype: int """ N=1 while n>0: r= 9* 10**(N-1)*N if n>r: n-=r N+=1 else: number= 10**(N-1) + (n-1)/N return int(str(number)[(n-1)%N])
相关文章推荐
- 【leetcode】400. Nth Digit(Python & C++)
- [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 从1到n第n位数字是什么 + 找规律
- LeetCode 400. Nth Digit
- 【C语言】 LeetCode 400. Nth Digit
- leetcode 400. Nth Digit
- Leetcode 400. Nth Digit (Easy) (cpp)
- leetcode 400. Nth Digit
- [LeetCode]--400. Nth Digit
- LeetCode 400. Nth Digit
- LeetCode 400. Nth Digit
- leetcode 400. Nth Digit
- Leetcode 400. Nth Digit[easy]
- LeetCode 400. Nth Digit
- 【Leetcode】400. Nth Digit