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. 转载 HTTPS 之fiddler抓包、jmeter请求

    转载自 http://suixiang0923.github.io/2016/01/12/%E6%B5%85%E8%B0%88HTTPS%E4%BB%A5%E5%8F%8AFiddler%E6%8A% ...

  2. UESTC 1511(差分约束)

    题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...

  3. pythonanywhere笔记

    https://www.pythonanywhere.com Deploying an existing Django project on PythonAnywhere Deploying a Dj ...

  4. Angular2 兼容 UC浏览器、QQ浏览器、猎豹浏览器

    找到/src/polyfills.ts文件 把/** IE9, IE10 and IE11 requires all of the following polyfills. **/下注释掉的代码恢复 ...

  5. windows 2003 发布遇到问题---分析器错误消息: 未能加载类型“YWPT.MvcApplication”。

    问题如下: “/”应用程序中的服务器错误. ------------------------------------------------------------------------------ ...

  6. 使用ProcDump工具抓取dump

    首先得到要抓取的进程号 cd %windir%\syswow64\inetsrvappcmd list wp得到pid之后, 在任务管理器里发现w3wp.exe的CPU总在49%-60%左右, 间歇性 ...

  7. PMP十大知识领域整理

    2018-7-28至2018-12-8历时4个多月,学写了PMP(拍马屁),感觉自己经历了,哇-唉-哦-嗯这四个阶段 刚开始觉得如遇圣经,被PMP的知识体系和老师的精彩课程深深震撼! 后来觉得很多东西 ...

  8. 160301、js倒计时,页面上显示时间

    js: //倒计时 var countdown=60,t; function settime(){ if (countdown == 0) { $("#validateBtn"). ...

  9. Java IO 修改文件名

    /** *//**文件重命名 * @param path 文件目录 * @param oldname 原来的文件名 * @param newname 新文件名 */ public void renam ...

  10. Nginx无法启动,80端口被PID=4占用

    在nginx启动后,error.log中总是显示 80 端口被占用. 通过netstat -ano发现,其被一个叫PID=4的系统服务占用. 网上大多数的方法是说通过regidit修改注册表的方式解决 ...