题目:

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. c#远程链接服务器中MySQL

    转自原文 c#远程链接服务器中MySQL 1.要连接MySQL数据库必须首先下载mysql官方的连接.net的文件,文件下载地址为http://dev.mysql.com/downloads/conn ...

  2. HDU 4352

    #include <iostream> #include <cstdio> #include <cmath> #include <string.h> # ...

  3. Unity3D - 图形性能优化:优化着色器载入时间

    Unity官方文档之"图形性能优化-优化着色器载入时间"的翻译,E文链接. Optimizing Shader Load Time 优化着色器载入时间 Shaders are sm ...

  4. Ubuntu下的用户和权限(三)

    七.增删群组相关的命令 相同的我们要先介绍两个重要的设定档:/etc/group和/etc/gshadow,前面那个事实上和/etc/passwd一样.而后者就是群组的password表了.先看看长啥 ...

  5. JS的 验证组织机构的合法性

    以下直接上代码 //验证组织机构合法性方法 function orgcodevalidate(value){ if(value!=""){ var values=value.spl ...

  6. java中字符串编码转换

    Java 正确的做字符串编码转换 字符串的内部表示? 字符串在java中统一用unicode表示( 即utf-16 LE) , 对于 String s = "你好哦!"; 如果源码 ...

  7. ThinkPHP5 (路径优化,路由)

    路径:www.tp5.comm/index.php/index/index/index 站点路径/入口文件/模块/控制器/方法 一.绑定模块 public下的php文件,如index.php,内部写 ...

  8. SpringBoot(十一) Dubbo分布式与Zookeeper

    Dubbo简介 1.Dubbo简介 1. Dubbo是什么? dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上 ...

  9. 五分钟DBA:浅谈伪分布式数据库架构

    [IT168 技术]12月25日消息,2010互联网行业技术研讨峰会今日在上海华东理工大学召开.本次峰会以“互联网行业应用最佳实践”为主题,定位于互联网架构设计.应用开发.应用运维管理,同时,峰会邀请 ...

  10. 01《UML大战需求分析》阅读笔记之一

    在大二的时候就已经在课堂上对UML也就是统一建模语言有了初步的了解,但是却不怎么明白,虽然可以画图可以完成任务,但是有些糊里糊涂.所以特地把这门书作为精读书籍,想要更加深度地学习UML.很多内容只用语 ...