数字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. 域名IP主动验证(一)

    功能:主动验证给定的域名.IP对是否真正的关联 思路: 1.一开始通过修改hosts文件,把待验证的域名.IP对添加到文件里,然后用wget尝试访问,再恢复hosts文件重新验证下一对 2.后来了解到 ...

  2. AJPFX总结Collection集合(上)

    出现集合类的原因 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一个方式. 数组和集合都是容器有何不同? 数组虽也可存储对象,但长度 ...

  3. Spring Cloud Config 使用Bus的动态配置中心

    server端配置 POM文件 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  4. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  5. Macbook air 上打开cocoscreator出错

    Error: EROFS: read-only file system, open '/Volumes/Cocos Creator/CocosCreator.app/Contents/Resource ...

  6. joomla多语言建站之默认前台语言设置

    joomla多语言建站后,如果想设置其中一种语言为默认前台语言,即: 从后台点击“Site Name”跳转时: 访问域名时: 页面自动切换至某一种语言,可以在后台通过“语言管理”模块来实现,将“网站前 ...

  7. ag-grid-vue的 行默认选中

    that.$nextTick(() => { that.gridListOptions.api.onGroupExpandedOrCollapsed(); that.$nextTick(() = ...

  8. Really simple SSH proxy (SOCKS5)

    原文: https://thomashunter.name/blog/really-simple-ssh-proxy-socks5/ SOCKS5 is a simple, eloquent meth ...

  9. 腾讯AI开放平台的接口调用指南

    最近无意发现腾讯AI开放平台上提供了大量好玩的人工智能云服务,而且是完全免费的.只需要用QQ号登录即可.这么好的东西,作为一个程序员,当然要试试了! 从上图可以看出腾讯AI开放平台提供的人工智能服务主 ...

  10. Image Is Everything LA2995

    白书第一章例题6 构造.思维.几何. 分别从几个角度去看,有矛盾就删掉,最后遍历一下统计个数 方法证明:第一个方块肯定要删除.假设前k个必须删除,第k+1个矛盾出现,假如不删掉,矛盾将持续存在,故必须 ...