题意:给出一个整数n,求从0到n之间的数中出现1的总个数

思路: 根据   high cur low(其中cur是1位),而number表示cur当前的位数(1,10,100,。。。)

当cur=0时,则有high * number

当cur =1时,则有high * number + (low + 1)

当cur>1时,则有(high+1) * number

代码如下:

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

LeetCode Number of Digit One(计算1的个数)相关推荐

  1. [LeetCode]Number of Digit One,解题报告

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

  2. LeetCode Number of Digit One

    原题链接在这里:https://leetcode.com/problems/number-of-digit-one/ 每10个数, 有一个个位是1, 每100个数, 有10个十位是1, 每1000个数 ...

  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. LeetCodeOJ.Number of Digit One

    试题请参见: https://leetcode.com/problems/number-of-digit-one/ 题目概述 Given an integer n, count the total n ...

  5. [LeetCode] Number of 1 Bits Reverse Integer - 整数问题系列

    目录: 1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历] 3.Reverse Integer - 翻 ...

  6. LeetCode Number of Islands(flood fill)

    问题:给出一个由0和1组成的二维网格图(m*n),1表示陆地,0表示水.要求统计有多少块陆地 思路:常见的flood fill算法有三种,深度优先搜索.广度优先搜索以及广度扫描法.广度扫描法其实原理与 ...

  7. C++ number of positive divisors计算正除数的实现算法(附完整源码)

    C++number of positive divisors计算正除数的实现算法 C++number of positive divisors计算正除数的实现算法完整源码(定义,实现,main函数测试 ...

  8. tensoflow_yolov3 计算平均识别个数(平均识别数)

    # -*- coding: utf-8 -*- """ @File : 20200221_Target_Recognition_光照度对模型识别率影响(计算平均识别个数) ...

  9. R语言stringr包str_count函数计算字符串匹配个数实战

    R语言stringr包str_count函数计算字符串匹配个数实战 目录 R语言stringr包str_count函数计算字符串匹配个数实战 #导入stringr包 #仿真数据

最新文章

  1. oracle11中过程continue,Oracle 11g OCM备考之创建EM与EM登陆异常的处理
  2. Python 生成exe文件报错:ImportError: unable to find Qt5Core.dll on PATH
  3. 用TensorFlow可视化卷积层的方法
  4. 一个能描述erp系统的小故事。
  5. 惊叹C4D设计作品分享,超级给力的灵感
  6. 神经网络的Hello World
  7. 动画效果之PC端 移动端 探照灯遮罩动画
  8. 关于WPF绘图中的path.data在后台重新赋值的语法
  9. MapX bug 和设计缺陷
  10. Vlan中 tagged和untagged的区别
  11. 在IntelliJ IDEA下编辑jspx文件(转)
  12. 瑞星linux病毒库位置,瑞星杀毒软件V16+怎样离线升级病毒库 病毒库离线升级办法...
  13. qt界面中Pushbutton添加图片的三种显示效果
  14. JSP空间上当受骗篇
  15. 【端口被占用】查看占用程序,并结束占用程序、MySQL的Unable to connect to the database问题
  16. 《网络攻防》网络欺诈技术防范
  17. DBFS解二阶魔方——一次c++学习之旅
  18. Linux usb设备驱动(2)--- usbmouse.c 源码分析
  19. 联想收购方正,不是不可能
  20. Python中读写文件、异常和异常处理、模块和包

热门文章

  1. css selector list
  2. 百度 php 图片文字识别,PHP实现百度OCR文字识别
  3. python考证什么时候报名-2019年3月上海计算机二级考试报名时间安排 什么时候报名...
  4. python下载网页里面所有的图片-python批量下载图片的三种方法
  5. python课程推荐-课程推荐:四天人工智能 python入门体验课
  6. 用python绘制柱状图标题-Python笔记:用Python绘制炫酷的柱形图
  7. python3.8.2安装教程-在服务器上安装python3.8.2环境
  8. python界面图片-python实现读取并显示图片方法(不需要图形界面)
  9. python项目实例初学者-经典Python案例,初学者的小帮手,立马学会Python!
  10. python散点图点的大小-python散点图面积大小比例轴长度