题目:

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.

思路:

对这个数字的每一位求存在1的数字的个数。

从个位開始到最高位。

举个样例54215。比方如今求百位上的1,54215的百位上是2。能够看到xx100到xx199的百位上都是1,这里xx从0到54。即100->199, 1100->1199...54100->54199, 这些数的百位都是1,因此百位上的1总数是55*100



假设n是54125,这时因为它的百位是1,先看xx100到xx199。当中xx是0到53,即54*100, 然后看54100到54125,这是26个。所以百位上的1的总数是54*100 + 26.



假设n是54025。那么仅仅须要看xx100到xx199中百位上的1,这里xx从0到53,总数为54*100

求其它位的1的个数的方法是一样的。

代码:

class Solution {
public:
int countDigitOne(int n)
{
int res=0;
long left, right, base=1;
if (n <= 0)
return 0;
while (n >= base)
{
left = n / base; //left包括当前位
right = n % base; //right为当前位的右半边 if ((left % 10) > 1)
res+= (left / 10 + 1) * base; else if ((left % 10) == 1)
res+= (left / 10) * base+ (right + 1); else
res+= (left / 10) * base;
base *= 10;
}
return res;
} };

能够把上面三个条件合成一步。例如以下:

class Solution {
public:
int countDigitOne(int n)
{
int res=0;
long left, right, base=1;
if (n<=0)
return 0;
while (n>=base)
{
left = n / base; //left包括当前位
right = n % base; //right为当前位的右半边 res += ((left + 8) / 10 * base) + (left % 10 == 1) * (right + 1);
base *= 10;
}
return res;
} };

LeetCode OJ 之 Number of Digit One (数字1的个数)的更多相关文章

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

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

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

  3. 【LeetCode】233. Number of Digit One

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

  4. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  5. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  6. [LeetCode] 137. Single Number II 单独的数字之二

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  7. LeetCode OJ -Happy Number

    题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...

  8. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. LeetCode 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

随机推荐

  1. Linux--对文件夹下的配置文件批量改动IP

    sed -i 's/10.1.1.1/10.1.1.2/g' `grep -ir 10.1.1.1 * |grep -E '.xml:|.cfg:|.ini:|.wsdl:|.properties:' ...

  2. 兔子-RadioButton和RadioGroup的关系

    RadioButton和RadioGroup的关系: 1.RadioButton表示单个圆形单选框.而RadioGroup是能够容纳多个RadioButton的容器 2.每一个RadioGroup中的 ...

  3. WebService概述和CXF入门小程序

    一. 什么是WedService? WebService不是框架, 甚至不是一种技术, 而是一种跨平台,跨语言的规范, WebService的出现是为了解决这种需求场景: 不同平台, 不同语言所编写的 ...

  4. Android nomedia 避免图片等资源泄露在系统图库其中

    总结 Android nomedia 避免文件泄露在系统图库和系统铃声中 在应用开发中 项目的图片总是被系统的图库收录了 避免图片被系统图库收录的发现有2个方法 第一种针对图片 将 .png为后缀的图 ...

  5. 【iOS开发系列】UIDevice设备信息

    [1] 推断是否是横向屏: BOOL b=UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation); 获取设备uniqu ...

  6. Oracle新建Schema

    1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...

  7. elasticsearch源码分析之search模块(client端)

    elasticsearch源码分析之search模块(client端) 注意,我这里所说的都是通过rest api来做的搜索,所以对于接收到请求的节点,我姑且将之称之为client端,其主要的功能我们 ...

  8. ACM/OI 出题用

    之前出题,很苦恼出数据和检查程序,因为很多繁琐的工作,还很可能小手一抖出问题. 最近又在出题...想起之前的对拍脚本,感觉不能更方便,于是撸了一套出题用的小工具,也学习了一点点的DOS命令 首先是输入 ...

  9. php的异步并行扩展swoole

    Swoole是PHP的异步并行扩展,有点像Node.js,但swoole既支持同步又支持异步,比node更强大.Swoole扩展是基于epoll高性能事件轮询,并且是多线程的,性能非常好. Swool ...

  10. tableview偏移

    tableview偏移 方法一:改变edgesForExtendedLayout self.edgesForExtendedLayout = UIRectEdgeNone; 将edgesForExte ...