233. Number of Digit One(统计1出现的次数)
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出现的次数)的更多相关文章
- 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 ...
- 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 ...
- (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 ...
- 233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 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 ...
- 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 ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- [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 ...
随机推荐
- hdu 1560(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较 ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- uilabel 和uitextview 自适应大小
本文转载至 http://blog.csdn.net/liulichao20/article/details/8957752 分类: ios2013-05-21 22:06 321人阅读 评论(0) ...
- poj_2559 单调栈
题目大意 给出一个柱形图中柱子的高度,每个柱子的宽度为1,柱子相邻.求出柱形图中可能形成的矩形的最大面积. 题目分析 以每个柱子(高度为h[i])为中心,向两边延展求出以该h[i]为高度的矩形的最大宽 ...
- 关于PostgreSQL的SQL注入必知必会
一.postgresql简介 postgresql是一款关系型数据库,广泛应用在web编程当中,由于其语法与MySQL不尽相同,所以其SQL注入又自成一派. 二.postgresql的SQL注入:(注 ...
- Excel表导出
前言 分别介绍两种导出Exce表格的方法和读取Excel表格数据方法. 1.在MVC下的表格导出. 2.基于NPOI的表格导出. 3.读取Excel表格数据. 第一种方法:在MVC下的表格导出. 首 ...
- kafka 相关配置
kafka主要配置包括三类:broker configuration,producer configuration and consumer configuration. Broker Config ...
- 解析oracle的rownum,数据库查询结果返回行数设置
对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,而且rownum不能以任何表的名称作为前缀 ...
- json、jsonp的定义和区别
一.区别 简单来说,json是一种数据交换格式,jsonp是一种非官方跨域数据交互协议.json描述的是信息的格式,而jsonp是信息传递双方约定的方法.json返回的是一串数据,而 jsonp返回的 ...
- Windows配置MinGW环境变量
先安装MinGW 1.添加3个系统变量(根据自己的实际路径) MinGW_INCLUDE_PATH MinGW_LIBRARY_PATH MinGW_PATH 2.将MinGW_PATH添加到Path ...