数字1的个数

给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。

示例:

输入: 13

输出: 6

解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。

1的总个数为1在1~n所有数中

个位数上有1的个数+十位数上有1的个数+...+亿位数上有1的个数+...

自己动手亲自找一遍规律就能得出答案:

首先,找规律:

13

个位数为1:1 11

十位数为1:10 11 12 13

1的总个数为: 2+4=6

23

个位数为1:1  11 21

十位数为1:10 11 12 13 14 15 16 17 18 19

1的总个数为:3+10=13

345

个位数为1:1 11 21 31 41 51 61 71 81 91 101 111 121 131 141   ...341

十位数为1:10 11 12 13 14 15 16 17 18 19  ...311 312 ...319

百位数为1:100  101...199

1的总个数为:100+40+35=175

进而可得通项:

通项:求某一位的1的个数

高n位*本位(比如百位就乘100)+  0                             (本位小于1)

1*本位                     (本位大于1)

低n位+1                  (本位等于1)

也就是说,某位(各位,十位...)1的总个数可能与其高位,低位以及自己的

值有关,具体对应情况如上

例如算12345:

个位1:1234*1+1(个位>=1加1)

十位1:123*10+10

百位1:12*100+100

千位1:1*1000+1000

万位1:2345+1

1的总个数为:8121

例如算23012:

个位1:2301*1+1

十位1:230*10+2+1  (十位=1加低位即2然后加1)

百位1:23*100            (百位为0加0)

千位1:2*1000+1000

万位1:10000

1的总个数为:19905

通俗来说,某位(个位,十位..)上1的个数=

基础数+当前位为>0,<0,=0时的情况,

而基础数为当前位前面的高位*当前位

(例如:23012,当 当前位为百位时,基础数=23(前高位)*100+上面讨论的情况)

 class Solution {
public:
int countDigitOne(int n) {
int k = 1, sum = 0, curr, large, small = 0;
while (n>0){
curr = n % 10;
large = n / 10;
if (curr>1)
sum = sum + large*k + k;
else if (curr<1)
sum += large*k;
else
sum += large*k + small + 1;
small = small + curr*k;
n = n / 10;
k = k * 10;
}
return sum;
}
};

Leetcode 233.数字1的个数的更多相关文章

  1. Java实现 LeetCode 233 数字 1 的个数

    233. 数字 1 的个数 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 1 ...

  2. leetcode 233. 数字 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. Magento 缓存机制简析

    在知道缓存机制前,首先需要知道,Magento的路由机制,这边就不做赘述了,百度一大堆. 下面一个简单的缓存生效流程: A:首先在页面开始时,Magento在app\code\core\Mage\Co ...

  2. IOS-关闭(退)键盘事件--转

    方法: 1.手势(触背景)关闭键盘 -(void)tapBackground //在ViewDidLoad中调用{    UITapGestureRecognizer * tap = [[UITapG ...

  3. AJPFX分享eclipse自动生成java注释方法

    设置方法介绍:eclipse中:Windows->Preferences->Java->Code Style->Code Template->Comments,然后对应的 ...

  4. T4312 最大出栈顺序

    题目描述 给你一个栈和n个数,按照n个数的顺序入栈,你可以选择在任何时候将数 出栈,使得出栈的序列的字典序最大. 输入输出格式 输入格式: 输入共2行. 第一行个整数n,表示入栈序列长度. 第二行包含 ...

  5. bt5r3安装postgresql

    apt-get install postgresql

  6. CSV解析

    NSString *path = [[NSBundle mainBundle] pathForResource: @"type" ofType:@"csv"]; ...

  7. win驱动安装记录

    工具:devcon64.exe 安装/更新/删除等记录:c:\windows\inf\setupapi.dev.log

  8. AngularJS日期格式化

    本地化日期格式化:({{ today | date:'medium' }})    Mar 28, 2016 6:42:25 PM({{ today | date:'short' }})   3/28 ...

  9. MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据

    前两篇教程我们介绍了如何搭建MongoDB的本地环境: MongoDB最简单的入门教程之一 环境搭建 以及如何用nodejs读取MongoDB里的记录: MongoDB最简单的入门教程之二 使用nod ...

  10. HttpClient 接口调用

    String url = "http://127.0.0.1:8080/api"; //然后根据表名获取公司信息 HttpPost httppost = new HttpPost( ...