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.

Hint:

  1. Beware of overflow.
class Solution {
public:
int countDigitOne(int n) {
if(n <= )
return ;
vector<int> v;
int t = n;
while(t)
{
v.push_back(t%);
t /= ;
}
int l = v.size(), rest = n - v[l-] * pow(, l-);
return (v[l-] > ? pow(, l-) : rest + ) + v[l-] * (l-) * pow(, l-) + countDigitOne(rest);
}
};

233. Number of Digit One *HARD* -- 从1到n的整数中数字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. (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 ...

  3. 233. Number of Digit One

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

  4. 【LeetCode】233. Number of Digit One

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

  5. 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 ...

  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. 【转载】.NET面试题系列[0] - 写在前面

    原文:.NET面试题系列[0] - 写在前面 索引: .NET框架基础知识[1] - .NET框架基础知识(1) http://www.cnblogs.com/haoyifei/p/5643689.h ...

  2. centos7 安装教程

    1. 在安装的时候,不要用默认的最小安装.选择GNOME Desktop 2.自动分区的时候,选择自己进行分区 2个分区,1个大小为1024M的swap分区,剩下的分配一个ext3的分区.设备类型都选 ...

  3. 长链非编码RNA(lncRNA)

    长链非编码RNA(lncRNA) 转自:http://blog.sina.com.cn/s/blog_909da11301010bkz.html     长链非编码RNA(lncRNA)是一类转录本长 ...

  4. [HDOJ5935]Car(精度,数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935 题意:有个老司机在开车, 开车过程中车的速度是不减的. 交警记录了这个老司机在nn个时间点的位置 ...

  5. CUBRID学习笔记 35 net驱动错误码和信息 cubrid教程示例

    DO.NET Error Code Number Error Code Error Message Note 0 ER_NO_ERROR "No Error"   1 ER_NOT ...

  6. WCF学习资源收集汇总

    1.WCF编程 http://www.cnblogs.com/wengyuli/category/217446.html 2.wcf热门问题编程示例 http://blog.csdn.net/book ...

  7. ZOJ-2562 More Divisors 反素数

    题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最大的一个. 分析:反素数定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4 ...

  8. E2 2014.6.3 更新日志

    增加功能 增加支持中关村获取商品信息 增加个人业绩查询功能 增加赠送和获赠查询功能 增加商品历程分析报表,资金历程分析报表,科目明细分析报表, 销售分析报表 增加服务维修明细表查询报表 完善功能 固定 ...

  9. main函数中argc理解

    其实: int main(int argc,char *argv[])是UNIX和Linux中的标准写法,而int main()只是UNIX及Linux默许的用法..void main(int arg ...

  10. Python变量类型

    Python变量类型 变量是存储在内存中的值,因此在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定的内存,并决定什么数据可以被存储在内存中. 因此变量可以指定不同的数据类型, ...