Leetcode233-Number of Digit One

C++版 - 剑指offer 面试题32:从1到n整数中1出现的次数(leecode233. Number of Digit One) 题解 剑指offer 面试题32:从1到n整数中1出现的次数(Leecode233. Number of Digit One) 提交网址: http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6?tpId=13&tqId=11184 题目: 输入一个整数n,求从1到n这n个整数的十进 阅读详情

欢迎参观我的博客:Huhaoyu’s Blog。

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

For example:

Given n = 13,

Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

分析

题意如下:给定一个非负整数n,输出1,2,3,4,...,n这n个数中出现1的次数。

一开始的思路是遍历从1到n这n个数,数出每个数中1的个数,时间复杂度O(nlogn)。看到较低的通过率,估计这种思路行不通。测试发现当n较大时会超时。代码如下:

class Solution {
public:
    int countDigitOne(int n) {
        int count = 0;
        for (int i = 1; i <= n; ++i) {
            int num = i;
            while (num) {
                if (num % 10 == 1) {
                    ++count;
                }
                num /= 10;
            }
        }
        return count;
    }
};

因此换一种思路:

先从中找规律。比如n=121。我们可以试图逐位数出1的个数。

  • 首先是个位。
  • 1,11,21,31,41,51,61,71,81,91,101,111,121。不难发现是12+1个“个位”的1。
  • 十位。
  • 10,11,12,13,14,15,16,17,18,19,110,111,112,113,114,115,116,117,118,119。2*10个“十位”的1。
  • …
  • 不难发现对于第i位。
  • 我们记数字n = a(m) * 10^m + a(m-1) * 10^(m-1) + ... + a(0) * 10^0。
  • 并记p和q分别为第i位前的数和第i位后的数。
  • p = a(m) * 10^(m-i-1) + ... + a(i+1) * 10^0。
  • q = a(i-1) * 10^(i-1) + ... + a(0) * 1。
  • 再记第i位数字为k。
  • 第i位的1的个数:p * 10^(i-1) + if (k == 1) { (q+1); } + if (k > 1) { 10^(i-1); }
  • 如数字83121=8 * 10000 + 3 * 1000 + 1*100 + 2 * 10 + 1 * 1, 对第三位(第三位为1)有p=83,q=21,k=1。第3位的1的个数为83 * 100 + 21 + 1 = 8322。
  • 不难发现时间复杂度为O(logn)。

AC代码

class Solution {
public:
    int countDigitOne(int n) {
        int count = 0, previous = 0, coef = 1;
        while (n) {
            int remain = n % 10;
            int over = n / 10;
            if (remain > 1) {
                count += coef;
            } else if (remain == 1) {
                count += previous + 1;
            }
            count += coef * over;
            previous += coef * remain;
            coef *= 10;
            n /= 10;
        }
        return count;
    }
};

如分析或代码有误,请批评指正,谢谢。

搞定汽车座椅电机EMC超标:一个PCB滤波板的实战设计与避坑心得 本文详细介绍了汽车座椅电机EMC超标问题的解决方案,通过PCB滤波板的实战设计,解决了传导发射测试超标问题。文章分享了在空间受限条件下的滤波电路架构设计、接地系统优化及迭代验证过程,为汽车直流电机的EMC整改提供了实用方法论。 阅读详情

相关推荐

SPL06气压温度传感器SPI驱动与补偿算法详解

气压传感器与温度传感器是嵌入式环境感知系统的核心组件,其测量精度高度依赖于MEMS芯片的校准参数与非线性补偿机制。SPL06作为集成式高精度数字传感器,采用24位Σ-Δ ADC与片内补偿引擎,需结合唯一出厂校准系数(即‘芯片指纹’)进行多阶数学修正,才能将原始ADC值转化为符合计量要求的帕斯卡(Pa)与摄氏度(℃)物理量。该过程涵盖SPI通信协议解析、寄存器时序控制、大端数据拼接、二进制补码处理及浮点/定点混合运算等典型嵌入式工程实践。广泛应用于无人机高度计、智能穿戴海拔监测与IoT气象终端中。

weixin_34640289的博客 142

leetcode 233 - Number of Digit One

转载出处 http://www.cnblogs.com/grandyang/p/4629032.html 这篇博客主要是提醒自己下次要弄明白这道题,现在还是不大懂 Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal

知足常乐,知行合一 358

WGCNA分析中那些没人告诉你的坑:以肺结核差异基因模块筛选为例

本文深入探讨WGCNA分析在肺结核差异基因模块筛选中的七个关键陷阱,包括soft-thresholding选取、样本离群值检测、模块合并阈值调整等。通过实际案例和R代码示例,揭示生信数据挖掘中常被忽视的技术细节,帮助研究者避免常见错误,提升分析结果的可靠性和生物学意义。

weixin_29218509的博客 261

233: Number of Digit One

转载:http://blog.csdn.net/xudli/article/details/46798619 Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given

simple008的博客 302

杂货店商品数据集挖掘关联规则实验

(2)选择事务集中前100条事务,选择合适的支持度阈值,使用Apriori算法和FP-growth算法挖掘频繁项集(推荐使用mlxtend包的apriori和fpgrowth函数),并对比apriori算法和fpgrowth算法的时间复杂度;(比如用户1000在2015年3月15日对应的事务为{‘sausage’,’whole milk’,’semi-finished bread’,’yogurt’}),然后按照用户ID递增,时间递增的方式对事务进行排序;7. 排版工整自洽,图表标题准确(10分)

m0_62500122的博客 1441

别再只看读写速度了!聊聊手机闪存eMMC、UFS3.1和SSD背后的功耗与体积玄学

本文深入探讨手机闪存技术eMMC、UFS3.1和SSD在功耗与体积设计上的关键差异。通过对比接口技术、封装工艺和温度管理,揭示移动存储如何在有限空间内平衡性能与能效,为消费者提供选购参考。重点关注UFS3.1的差分信号设计和全双工架构带来的显著优势。

weixin_27061475的博客 444

233 - Number of Digit One

Number of Digit One  Total Accepted: 307 Total Submissions: 1853 Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

静水流深 2414

详解Σ-Δ型ADC拓扑结构的基本原理

现在,总环路滤波器的H(f)具有更大的滚降,Mod 2型的噪声传递函数具有40 dB/十倍频程的上升速率。同样,当正弦波输入为负满量程时,+1和–1之间的转换会减少,输出以–1为主。噪声传递函数是一个高通滤波器函数,提供噪声整形,在DC附近的较低频率,对量化噪声有很强的抑制。H(f)是环路滤波器的函数,定义噪声和信号的传递函数。Σ-Δ型ADC的噪底由热噪声决定,而不受量化噪声的限制。量化噪声为量化器的输入与输出之差。图5. Mod 1 Σ-Δ环路的线性模型(b),包括方程、滤波器、信号和噪声传递函数图。

qq_43416206的博客 1809

233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the followin

我在天上飘的博客 248

SAP会计凭证导入ABAP开发模板+注意点—附带源码

开发并不仅仅只要考虑实现的功能,只实现功能的是程序,考虑到使用场景的是工程。使用bapi校验创建会计凭证的数据,报错和参数和正式创建一致。用途,防止产生错误的凭证,即使财务凭证可以冲销。正式创建会计凭证。隐藏的坑。

小九啥也不会 1232

YOLOv8进阶实战:融合SAHI超推理算法,在无人机应用中精准捕捉视频与图片中的微小目标

YOLOv8改进是一种先进的目标检测算法,通过引入辅助超推理算法SAHI推理,使得小目标无法逃避检测。SAHI推理算法结合视频和图片的特点,利用先进的超推理技术,提高了目标检测的准确性和鲁棒性。不仅如此,YOLOv8改进还支持对视频和图片进行实时的目标检测,将检测结果直观地呈现给用户。这一技术的应用范围广泛,可以用于安防领域,提升监控设备的性能和效率;也可以用于无人驾驶领域,实现精准的目标识别和跟踪。总之,YOLOv8改进是一项具有突破性的技术,为目标检测领域带来了重大的进展。

weixin_51988935的博客 806

Leetcode 233. Number of Digit One

题目描述:给定n,找出小于n的数字中含1的个数。 题目链接:Leetcode 233. Number of Digit One reference: https://leetcode.com/discuss/44281/4-lines-o-log-n-c-java-python intuitive: 每10个数, 有一个个位是1, 每100个数, 有10个十位是1, 每1000个数, 有10...

Za七杂⑧ 182

Claude Code 100个真实案例 - 用AI搭建GIS地理信息系统(地图标注+热力图)

本案例展示如何使用 Claude Code 从零搭建一个完整的 GIS 地理信息系统。我们将使用 Python + Flask 作为后端,Leaflet.js 作为前端地图引擎,实现地图标注、热力图展示、区域绘制、坐标搜索等功能。全程通过与 Claude Code 对话完成,让你感受 AI 在地理信息领域的强大开发能力。

yueyeguzhuo的专栏 112

Python leetcode 233. Number of Digit One

python leetcode 233. Number of Digit One 题目 https://leetcode.com/problems/number-of-digit-one/ 思路 比如21345 先看1345~21345:万位上有一万个1(剩余四位从0000到9999),再看千位取1(万位取1或2,百十个位上取0~9都可以),再看百位与千位相同取法,同理十位与个位。然后是1...

weixin_41710583的博客 367

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode No.233 Number of Digit One 题解

LeetCode No.233 Number of Digit One 题解题目描述 Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13,

Guo15331092的博客 514

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

leetcode 233: Number of Digit One

leetcode 233: Number of Digit One java python c++

西施豆腐渣 1万+

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LeetCode 233. Number of Digit One

原题网址:https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Give

James Pan 875

【Leetcode】233. Number of Digit One

的每一位求出来然后从左到右排列。,那么除了上面那个方案之外,左边是可以取。对每一位求个总和即可。这些正整数中各个位总共有多少个。这一位,有多少个数能取。,那么其左边的方案数是。,那么其左边的方案是。的,而此时右边可以取。

数学、算法爱好者的博客 1185

LeetCode No.233 Number of Digit One

LeetCode No.233 Number of Digit One

黄健伟的博客 348

[leetcode] 233. Number of Digit One 解题报告

题目链接: https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Giv

小榕流光的专栏 1755

[leetcode] 233. Number of Digit One

Description Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. Example: Input: 13 Output: 6 Explanation: Digit 1 occurred in the ...

Learning from the mistakes 202

Leetcode233 Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the follow

suichen1的专栏 476

Leetcode 233 Number of Digit One

1. 问题描述  给定一个整数n,在所有不大于n的非负整数中,计算包含数字1的整数的个数。例如n=13n = 13的结果为6,包含1的数字有1,10,11,12,13。2. 方法与思路  这个问题最直观的方法就是累加1到n所有的包含1的数的个数。每个数通过循环取余10的方法判断是否包含1。但是这种思路效率并不高,数字n有lognlogn位,总得时间复杂度为O(nlogn)O(nlogn)。   利

Jeanphorn的专栏 5443

leetcode 233 number of digit one

假设n = xyzdabc,此时我们求千位是1的个数,也就是d所在的位置。 那么此时有三种情况, d == 0,那么千位上1的个数就是xyz * 1000 d == 1,那么千位上1的个数就是xyz * 1000 + abc + 1 d > 1,那么千位上1的个数就是xyz * 1000 + 1000 代码如下: public int countDi...

m0_37313888的博客 221

LeetCode刷题:233. Number of Digit One

LeetCode刷题:233. Number of Digit One 原题链接:https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or ...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 382
上一篇: Leetcode174-Dungeon Game
下一篇: 建堆O(n)时间复杂度证明
coderhuhy
博客等级 码龄12年 24粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值