1.算法说明:

如3141592,在m(digitDivide)=100时,即要求计算百位上“1”的个数

其中a为31415,b为92,31415中出现了3142次“1”,因为每10个数里面出现1次“1”。而实际上,31415是3141500,所以把31415中1的个数再乘以m。如3141400~3141499中,前缀为31414的数出现了100次,所以需要乘以m(此时是100)。

class Solution {
public:
int countDigitOne(int n) { int ans=0;
for(long long digitDivide=1;digitDivide<=n;digitDivide*=10)
{
int a=n/digitDivide;
int b=n%digitDivide;
ans+=(a+8)/10*digitDivide+(a%10==1)*(b+1);
}
return ans; }};

Number of Digit One(Medium)的更多相关文章

  1. (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 ...

  2. [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 ...

  3. [Leetcode] Number of Digit Ones

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

  4. Number of Digit One

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

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

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

  7. 233. Number of Digit One

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

  8. 【LeetCode】233. Number of Digit One

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

  9. [Swift]LeetCode233. 数字1的个数 | Number of Digit One

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

随机推荐

  1. java 实现每次从list中取5000条数据放入新list

    从list中取固定条数的数据放入新的list里 public static <T> List<List<T>> split(List<T> resLis ...

  2. StarUML类图相关——关联、聚合、组合、泛化、依赖、实现

    在阅读设计模式相关的书籍,或者其他一些项目.相关博客等等,经常会遇到类图,它对于一个类的信息,如变量.方法及其可见性,类与类(接口)之间的继承关系.依赖关系.聚合关系.组合关系等,都可以比较形象得当地 ...

  3. codeforces 596 C. p-binary

    题意:给你一个n和一个p,让你用 (2k+p)进制来表示n,找出用最少的(2k+p)来表示n. 分析:首先我们看到2k,首先下想到二进制,我们可以我们列出式子,也就是 (2x1 + p)+(2x2 + ...

  4. JetBrains系列-插件

       插件官网:http://plugins.jetbrains.com   注意:网站有时不稳定,会造成打不开,属正常现象或许下一秒就好了,可以选择去git等方式下载 1.安装说明:   打开fil ...

  5. sync实现windows与nginx主机端文件同步(参考文档)

    资源 Rsync官网:http://rsync.samba.org/ 简介 Rsync(remote sync)是类unix系统下的远程(LAN/WAN)数据镜像备份工具.可以实现Linux与linu ...

  6. split和join合写

    小骆驼 第一章 简介 1. // input 和截取字符 #!usr/bin/perl use strict; use warnings; while ( <> ) { chomp; pr ...

  7. 17.3.12----OS模块ya

    1---他是一xu个python系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. import os#导入这个os模块,其实python模块就是C元的包含很多函数的文件 2---o ...

  8. python字典常用方法

    字典(Dictionary) 字典是一个无序.可变和有索引的集合.在 Python 中,字典用花括号编写,拥有键和值. 实例 创建并打印字典: thisdict = { "brand&quo ...

  9. 实践一次有趣的sql优化

    课程表 #课程表 create table Course( c_id int PRIMARY KEY, name varchar(10) ) 增加 100 条数据 #增加课程表100条数据 DROP ...

  10. bzoj2127happiness(最小割)

    一眼最小割. 一种比较好想的建图方式如下: 连源点表示学文,连汇点表示学理,然后adde(S,id(i,j),a[i][j]),adde(id(i,j),T,b[i][j]):对于相邻座位选择同一科的 ...