问题描述

给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。
示例:
输入: 13
输出: 6
解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。

问题分析

这道题可以换一个思路,即[0,n]中有多少个小于n的第i位为1的数,例如n = 12345时,我们要找百位数为1时,存在多少数m小于12345,我们分为两部分,设\(m = p1q\),

  • 如果\(p\in[0,11]\)时,\(q\)可以取\([0,99]\)之间任意一个数,共100个,
  • 如果\(p = 12\)时,因为百位数字为\(3>1\),因此我们还是可以取\([0,99]\)之间任意一个数,共\(100\)个,但是假如\(n=12045\),这样的数是不存在的,因为任意\(121q>12045\),假如\(n=12145\),则只能取\([0,45]\)之间的数,共46个。

综上我们可以总结规律如下:我们统计从左到右的第i位为1小于n的数量时

  • 首先可以确定有\(\lfloor \frac{n}{10^{i+1}}\rfloor\times 10^i\)个数
  • 下面考虑第i位的数\(x = \lfloor\frac{n}{10^{i}}\rfloor\%10\),若\(x > 1\),则还会有\(10^i\)个数小于n,如果\(x = 1\),则会有\(n \% 10^i + 1\)个数小于n, 如果\(x = 0\),则不会有新的满足条件的数了

代码

class Solution {
public:
int countDigitOne(int n) {
int count=0,x;
long div = 1;
while(n>=div)
{
count += (n / (div*10))*div;
x = (n/div)%10;
if(x == 1)count += (n%div + 1)*x;
else if(x > 1) count += div;
div *= 10;
}
return count;
}
};

参考博客

leetcode 233. 数字 1 的个数的更多相关文章

  1. Java实现 LeetCode 233 数字 1 的个数

    233. 数字 1 的个数 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 1 ...

  2. Leetcode 233.数字1的个数

    数字1的个数 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 . ...

  3. C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解

    C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解 在线提交: https://leetcode.com/problems/bitwise-and-of-num ...

  4. LeetCode:有效三角形的个数【611】

    LeetCode:有效三角形的个数[611] 题目描述 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有 ...

  5. 计算1到N中包含数字1的个数

    转自:http://pandonix.iteye.com/blog/204840 Mark N为正整数,计算从1到N的所有整数中包含数字1的个数.比如,N=10,从1,2...10,包含有2个数字1. ...

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

  7. 233 Number of Digit One 数字1的个数

    给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...

  8. LeetCode OJ 之 Number of Digit One (数字1的个数)

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

  9. LeetCode() 数字1的个数

    int ones = 0; for (long m = 1; m <= n; m *= 10) { long a = n/m, b = n%m; ones += (a + 8) / 10 * m ...

随机推荐

  1. Python pyecharts绘制水球图

    一.水球图Liquid.add()方法简介 Liquid.add()方法签名add(name, data, shape='circle', liquid_color=None, is_liquid_a ...

  2. CF710C Magic Odd Square 题解

    Content 构造出一个 \(n\times n\) 的矩阵,使得这个矩阵由 \(1\sim n^2\) 这些数字组成,并且这个矩阵的每行,每列,以及对角线的和都为奇数. 数据范围:\(1\leqs ...

  3. java 图形化小工具Abstract Window Toolit ImageIO缩放图片,添加水印

    实现步骤: 读取图像Image src = ImageIO.read 创建目标图像BufferedImage distImage = new BufferedImage(dstWidth, dstHe ...

  4. thinkphp 5 在页面输出当前时间

    我遇到的使用场景是<input>默认为当前时间,代码如下: <input name="starttime" id="starttime" ty ...

  5. c++设计模式概述之代理

    代码写的不规范,目的是缩短文章篇幅,实际中请不要这样做. 1.模式的结构 代理模式的主要角色如下: A.抽象主题(Subject)类:通过接口或抽象类声明真实主题和代理对象实现的业务方法. B.真实主 ...

  6. 【LeetCode】781. Rabbits in Forest 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. Adversarial Defense by Restricting the Hidden Space of Deep Neural Networks

    目录 概 主要内容 Mustafa A., Khan S., Hayat M., Goecke R., Shen J., Shao L., Adversarial Defense by Restric ...

  8. c++定时器执行任务

    // // Created by leoxae on 19-9-2. // #ifndef KEEKOAIROBOT_TIMERTASKHELPER_H #define KEEKOAIROBOT_TI ...

  9. BUUCTF [极客大挑战 2019]Not Bad

    总的来说这是一个64位orw的题 开头先在主函数里分配了一个很大的空间 1 __int64 __fastcall main(int a1, char **a2, char **a3) 2 { 3 mm ...

  10. Ranger开源贡献统计

    统计一下自己在Ranger开源社区贡献的Issue数量, 开源社区的Issue主要分为New Feature,Bug,Improvement, 这三种都是和代码相关的,会直接修改开源项目的代码库, 还 ...