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. UML-架构分析-步骤

    1.识别->因素表 2.解决->技术备忘录 1).可靠性 2).法律问题 3).可适应性

  2. Json返回结果为null属性不显示解决

    import java.io.IOException; import org.springframework.boot.autoconfigure.condition.ConditionalOnMis ...

  3. 如何实现MVC ActionResult 返回类型为JavaScriptResult

    必需的js引用文件 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>@Scripts ...

  4. Redis5新特性Streams作消息队列

    前言 Redis 5 新特性中,Streams 数据结构的引入,可以说它是在本次迭代中最大特性.它使本次 5.x 版本迭代中,Redis 作为消息队列使用时,得到更完善,更强大的原生支持,其中尤为明显 ...

  5. 京东云数据库 RDS助力企业便捷运维

    iPhone6发布那年,京东在国贸等商圈送货最快速度数分钟,包括从下单到送达.这是一个极端的富含营销因素例子.即便如此,常态来看,隔天到货的这种业务模式,也是基于同样的支撑:营销业务.物流业务,大数据 ...

  6. 微服务项目开发学成在线_day03 CMS页面管理开发

    springboot引入mangodb依赖坐标:在spring-boot集成条件下,使用mongodb的DAO层开发. swagger查看接口文档,请求地址:http://localhost:3100 ...

  7. HTML引入文件/虚拟目录/绝对路径与相对路径

    此篇引见 相对路径和绝对路径的区别 1.绝对路径 使用方法:而绝对路径可以使用“\”或“/”字符作为目录的分隔字符 绝对路径是指文件在硬盘上真正存在的路径.例如 <body backround= ...

  8. StartDT AI Lab | 需求预测引擎如何助力线下零售业降本增效?

    在当下经济明显进入存量博弈的阶段,大到各经济体,小到企业,粗放的增长模式已不适宜持续,以往高增长的时代已经成为过去,亟需通过变革发掘新的增长点.对于竞争激烈的线下零售行业而言,则更需如此. 零售行业一 ...

  9. JAVA8 函数式接口

    一.什么是函数式接口 1.只包含一个抽象方法的接口,称为函数式接口. 2.你可以通过Lambda表达式来创建该接口的对象.(若Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进 ...

  10. 小程序中map的取值和赋值

    1.初始化 resultMap: { "near": [], "join": [], "publish": [] } 2.js中直接取 co ...