题目:

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. 用户体验之如何优化你的APP

    用户体验,速度为王,来几个优化APP“速度”的建议. 1.后台执行 毋庸多言,已是通常做法. 一般在执行下载任务时让其在后台运营,让用户有精力去做别的事情. 后端加载 2.提前显示 客户端与WEB的数 ...

  2. [MFC]透明图展示

    (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) 一般我们可见的图形RGB三元色.对Alpha通道的话.它不一定会显示到窗口中来. 在Wi ...

  3. 用jquery实现隐藏列表表单的显示关闭切换以及Ajax方式改动提交相应的那一行的改动内容。

    请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 先给大家看看图片效果,大 ...

  4. 【VC编程技巧】窗口☞3.5对单文档或者多文档程序制作启动画面

    (一)概要: 文章描写叙述了如何通过Visual C++ 2012或者Visual C++ .NET,为单文档或者多文档程序制作启动画面.在Microsoft Visual Studio 6.0中对于 ...

  5. 点击了一个link button,查看后台调用

    使用F12进行监视 本身是一个linkbutton,可以看到绑定了一个JavaScript <a id="gvStaticConnection_ctl02_fresh" hr ...

  6. Oracle 数据库勒索病毒 RushQL 处理办法

    处理办法来自Oracle 官方: https://blogs.oracle.com/cnsupport_news/%E5%AF%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A% ...

  7. Ubuntu下安装tim/QQ/微信

    一.安装deepin-wine环境: 上https://github.com/wszqkzqk/deepin-wine-ubuntu页面下载zip包(或用git方式克隆),在“下载”目录下原地解压即可 ...

  8. Repeater控件使用小结持续更新

    Repeater嵌套Repeater绑定数据 前台代码 <!--注意层级关系不要写错了--> <asp:Repeater ID="rpGroup" runat=& ...

  9. javascript中client()兼容性封装

    function client() { var clientWidth = window.innerWidth || document.documentElement.clientWidth || d ...

  10. WebView简单用法

    1.空布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andr ...