Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

按不同位置统计

31456 统计百位时:

(0-31) 1 (0-99) 32*100次

31156:

(0-30)1(0-99) + (31)  1  (0-56)  31*100+56+1次

31056:

(0-30)1  (0-99) _31*100 次

 class Solution:
def countDigitOne(self, n):
ones, wei = 0, 1
while wei <= n:
m = int(n / wei) % 10 # 求某位数字 if m > 1:
ones += (int(n / wei / 10) + 1) * wei
elif m == 1:
ones += (int(n / wei / 10)) * wei + (n % wei) + 1
else:
ones += (int(n / wei / 10)) * wei
wei *= 10
return int(ones)

233. Number of Digit One(统计1出现的次数)的更多相关文章

  1. Java for LeetCode 233 Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  2. 233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  3. (medium)LeetCode 233.Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. 233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  5. 【LeetCode】233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  6. LeetCode 233 Number of Digit One 某一范围内的整数包含1的数量

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  7. leetcode 233 Number of Digit One

    这题属于需要找规律的题.先想一下最简单的情形:N = 10^n - 1 记X[i]表示从1到10^i - 1中 1 的个数,则有如下递推公式:X[i] = 10 * X[i - 1] + 10^(i ...

  8. 233 Number of Digit One 数字1的个数

    给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...

  9. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

随机推荐

  1. ES5与ES6的继承

    JavaScript本身是一种神马语言: 提到继承,我们常常会联想到C#.java等面向对象的高级语言(当然还有C++),因为存在类的概念使得这些语言在实际的使用中抽象成为一个对象,即面向对象.Jav ...

  2. Unknown SSL protocol error in connection to xxx:443

    使用git从远程下载时,出现Unknown SSL protocol error in connection to xxx:443 错误. 很有可能是被墙在了外面,这里针对墙在外面的情况. 设置代理服 ...

  3. 学习《深入理解C#》—— 数据类型、排序和过滤 (第一章1.1---1.2)

    引言 在开始看这本书之前看过一些技术博客,填补自己对于一些知识点的不足.无意中发现了<深入理解C#>这本书,本书主要探讨C# 2.C# 3和C# 4的细节与特性,所以做了一下阅读笔记,欢迎 ...

  4. iOS开发之--去除按钮的点击效果

    Button.adjustsImageWhenHighlighted = NO; 去除按钮的点击效果,用这句代码就可以了!

  5. [黑金原创教程] FPGA那些事儿《设计篇 I》- 图像处理前夕

    简介 一本为入门图像处理的入门书,另外还教你徒手搭建平台(片上系统),内容请看目录. 注意 为了达到最好的实验的结果,请准备以下硬件. AX301开发板, OV7670摄像模块, VGA接口显示器, ...

  6. ios UITableView多选删除

    第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ...

  7. 前端性能优化-减少http请求,dns预解析,减少repaint和reflow

    前端性能优化方法: 一 . 减少http请求 (1)通过合并图片,减少请求,俗称css sprites(css精灵)css sprites (2)lazyload懒加载,在需要的时候再加载 1.定义: ...

  8. 从0到1实现SourceTree连接Gitlab

    见下面的链接 http://note.youdao.com/noteshare?id=3622d02a38464c524222ede1b4fb06d2 SourceTree下载地址:Windows V ...

  9. CMS 收集器整理

    基本说明: 目标:获取最短回收停顿时间 算法:标记-清除算法 线程:并发 步骤: 初始标记:(会STP) 标记一下 GC Roots 能直接关联到的对象,速度很快 并发标记:(耗时最长,且可与用户线程 ...

  10. Java之Tomcat、Dynamic web project与Servlet

    一.Tomcat配置 Conf   Config   configration   -->配置 Service.xml:用来配置Tomcat Tomcat_users.xml:用来配置Tomca ...