avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

Sword finger Offer 43. 1~n Integer 1 Number of times

  2024-01-01        
字数统计: 213字   |   阅读时长: 1min

Sword finger Offer 43. 1~n Integer 1 Number of times

1
2
3
4
5
6
7
8
class Solution:
def countDigitOne(self, n: int) -> int:
mulk = 1
ans = 0
while n >= mulk:
ans += (n//(mulk * 10)) * mulk + min(max(n % (mulk * 10) - mulk + 1, 0), mulk)
mulk *= 10
return ans

That day, I made a set of Xiaomi measurement questions.,The first one is calculation1~nmiddle1Number of times。

Deduction的题号yesSword fingeroffer 43
nyes12in the case of,1 10 11 12middle就出现了5Second-rate1。

因为那套题middle选择题都yes简单难度,So naturally I want to use violent algorithm to crack。

但yes拿到Deductionmiddle找相同的题的时候却发现标签yesdifficulty[Be ashamedR]。Surmo after timeout[picture2]。

Then I actually thought about it directly with mathematical methods,But why too lazy, don’t want to find the rules,[picture三]yes标答,I feel that I have to sink in the future and slowly go to do it.。
#Deduction #algorithm#Deductionalgorithm #Python

Error answer:

[]class Solution:
1
2
3
4
5
6
7
def countDigitOne(self, n: int) -> int:
flag = 0
for i in range(1, int(n)+1):
for j in ''.join(str(i)):
if j == '1':
flag += 1
return flag
  • Python
  • answer
  • solved
  • difficulty

扫一扫,分享到微信

微信分享二维码
977.Orderly array square
121.The best time for buying and selling stocks
目录
ahoh, this article has no catalog.

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号