问题描述

给定一个整数 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. 在【自定义列】中使用M函数(Power Query 之 M 语言)

    数据源: "品名"一列 目标: 提取品名中的首字符,生成新列:"品名简称" 解决方案: 在[自定义列]中使用M函数Text.Start 步骤: 打开[自定义列] ...

  2. dart系列之:安全看我,dart中的安全特性null safety

    目录 简介 Non-nullable类型 Nullable List Of Strings 和 List Of Nullable Strings !操作符 late关键字 总结 简介 在Dart 2. ...

  3. CF363A Soroban 题解

    Content 给出一个数 \(n\),请你用算盘来表示 \(n\). 这里的算盘和普通的算盘一样,只不过竖着摆放罢了.左边只有一个珠子,每个珠子表示 \(5\):右边有四个珠子,每个珠子表示 \(1 ...

  4. 如何用uniapp+vue开发自定义相机插件——拍照+录像功能

    调用手机的相机功能并实现拍照和录像是很多APP与插件都必不可少的一个功能,今天智密科技就来分享一下如何基于uniapp + vue实现自定义相机界面,并且实现: 1: 自定义拍照 2: 自定义录像 3 ...

  5. 8-1yum私有云仓库

    针对centos8的BaseOS.AppStream源 yum -y install httpd systemctl enable --now httpd mkdir -pv /var/www/htm ...

  6. JAVA根据A星算法规划起点到终点二维坐标的最短路径

    工具类 AStarUtil.java import java.util.*; import java.util.stream.Collectors; /** * A星算法工具类 */ public c ...

  7. JAVA实现导出excel功能,封装通用导出工具类

    引入maven <!--excel导出--> <dependency> <groupId>net.sourceforge.jexcelapi</groupId ...

  8. 【九度OJ】题目1431:Sort 解题报告

    [九度OJ]题目1431:Sort 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1431 题目描述: 给你n个 ...

  9. 【LeetCode】475. Heaters 解题报告(Python & C++)

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

  10. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...