Nth Digit
来源:互联网 发布:怎么注册淘宝账号 编辑:程序博客网 时间:2024/06/06 01:18
题目详情:https://leetcode.com/problems/nth-digit/description/
终于做对了。思路挺简单的,就是一个地方没想明白,当有余数的时候,应该是第n+1个数;余数为0的时候为第n个数,体现在代码中为:
if m==0: #如果余数为0,则是第n个数 x=n+pow(10,i)-1 #比如10是两位数中的第一个数,11是第二个数 else: #余数不为0的情况,则是第n+1个数 x=n+pow(10,i) #得到数字
10为两位数中的第一个数,11为第二数,以此类推。
# -*- coding:utf-8 -*-class Solution(object): def findNthDigit(self, n): """ :type n: int :rtype: int """ if n<=9: #n<=9的话,那么直接返回 return n s,i=0,0 #s存储数的个数。比如:10是两个数 base=9 while s<n: #base*pow(10,i)为(i+1)位数的个数,比如:2位数(10-99)的个数为90个数 #base*pow(10,i)*(i+1)为(i+1)位数中数的个数 #比如:2位数一共有90个数,数字的个数位90*2=180 s=s+base*(i+1) base=base*10 i=i+1 i=i-1 #多加了一次1,减掉 s=s-base/10*(i+1) #s也多加,减掉 n=n-s #n减掉s,例如:n=30,s为9。n=n-s=21 m=n%(i+1) #n对i+1,即位数,取余 n=n/(i+1) #n除以位数,得到第几个数 if m==0: #如果余数为0,则是第n个数 x=n+pow(10,i)-1 #比如10是2两位数中的第一个数,11是第二个数 else: #余数不为0的情况,则是第n+1个数 x=n+pow(10,i) #得到数字 digits=[] #存储各个位的位数 while x!=0: #得到各个位上的数 digits.append(x%10) x=x/10 if m==0: #如果m==0,那么为个数数字 return digits[0] return digits[len(digits)-m]
阅读全文
0 0
- Nth Digit
- Nth Digit
- Nth Digit
- Nth Digit
- Nth Digit
- find the nth digit
- find the nth digit
- find the nth digit
- find the nth digit
- hdoj1597find the nth digit
- find the nth digit
- find the nth digit
- 【Leetcode】400. Nth Digit
- 400. Nth Digit
- LeetCode 400 Nth Digit
- [Leetcode] Nth Digit
- leetcode 400. Nth Digit
- 400. Nth Digit
- Logstash中ruby filter的调试器
- [Winform]通过钩子监控键盘操作和鼠标移动
- android api学习笔记:service
- ios-NSOperation的服务质量和completionBlock
- python练习(九)
- Nth Digit
- scala中function的使用
- RFC7233 HTTP范围请求(Range Requests)
- jquery中的ajax-检查用户名是否占用
- 线段树--HDU--1166
- 常见的HTTP状态码(HTTP Status Code)说明
- 原生js输出直角三角形和金字塔效果
- 【XSY1552】自动机 构造
- [1] first blog