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. 用 HTML5+ payment方法支付宝支付遇到的坑

    用 HTML5+ payment方法碰到的第一个坑就是如果是支付宝的话签约那种支付方式. 因为 Dcloud的文档没有更新的原因你可以看到他们说的都是‘移动支付’,但是你去支付宝平台的时候看到的根本就 ...

  2. JAVA 并发编程-多个线程之间共享数据(六)

    多线程共享数据的方式: 1.假设每一个线程运行的代码同样.能够使用同一个Runnable对象,这个Runnable对象中有那个共享数据,比如,卖票系统就能够这么做. 2,假设每一个线程运行的代码不同. ...

  3. superresolution_v_2.0 Application超分辨率程序文档

    SUPERRESOLUTION GRAPHICAL USER INTERFACE DOCUMENTATION Contents 1.- How to use this application. 2.- ...

  4. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  5. Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索

    题目链接:http://codeforces.com/contest/686/problem/C题目大意:给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求 ...

  6. 利用CSS3制作淡入淡出动画效果

    CSS3新增动画属性“@-webkit-keyframes”,从字面就可以看出其含义——关键帧,这与Flash中的含义一致. 利用CSS3制作动画效果其原理与Flash一样,我们需要定义关键帧处的状态 ...

  7. js访问CSS最终计算样式

    所谓计算样式,就是嵌入式样式.外部样式表.内联样式综合的样式表现,那么如何来获取呢? "DOM2 级样式"增强了document.defaultView,提供了getCompute ...

  8. Web测试系列之测试工具

    一Web功能测试工具MAXQ MAXQ是开源的Web功能测试工具. MAXQ是开源的Web功能测试工具.他的特点:1)简单易学;2)是一个轻量级的Web功能测试工具;3)可以自动录制WebBrowse ...

  9. Powershell替代和截断——replace and substring

    一:截取一个字符串的尾部,替代字符串中的特定字符或替代字符串中特定位置的特定字符 $a="W.endy.chen.SHAO" $b=$a.Substring(0,$a.Length ...

  10. python console

    print(sys.stdout.encoding, locale.getpreferredencoding ()) windows console : chcp 65001; 在设置了这个环境变量时 ...