原题链接在这里:https://leetcode.com/problems/number-of-digit-one/

每10个数, 有一个个位是1, 每100个数, 有10个十位是1, 每1000个数, 有100个百位是1.  做一个循环, 每次计算单个位上1得总个数(个位,十位, 百位).

例子:

以算百位上1为例子:   假设百位上是0, 1, 和 >=2 三种情况:

case 1: n=3141092, a= 31410, b=92. 计算百位上1的个数应该为 3141 *100 次.

case 2: n=3141192, a= 31411, b=92. 计算百位上1的个数应该为 3141 *100 + (92+1) 次.

case 3: n=3141592, a= 31415, b=92. 计算百位上1的个数应该为 (3141+1) *100 次.

以上三种情况可以用 一个公式概括:(a + 8) / 10 * m + (a % 10 == 1) * (b + 1);

期中 (a+8)/10 是用来判断该位是否大于等于2.

AC Java:

 public class Solution {
public int countDigitOne(int n) {
//(a+8)/10*m + (a%10 == 1)*(b+1)
int res = 0;
for(long m = 1; m<=n; m*=10){
long a = n/m;
long b = n%m;
res+=(a+8)/10 * m;
if(a%10 == 1){
res+=(b+1);
}
}
return res;
}
}

LeetCode Number of Digit One的更多相关文章

  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. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  3. Java for LeetCode 233 Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. (medium)LeetCode 233.Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  5. 【LeetCode】233. Number of Digit One

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

  6. LeetCode 233 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. 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 ...

  8. 233. Number of Digit One

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

  9. [Swift]LeetCode233. 数字1的个数 | Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

随机推荐

  1. html5调用手机摄像头,实现拍照上传功能

    今天做手机网站,想实现手机扫描二维码功能.首先实现在浏览器中调用手机摄像头,实现拍照功能并且把拍下的照片显示在页面并上传到服务器上,然后再在服务器端进行分析. 首先实现在浏览器中调用摄像头,当然用现在 ...

  2. bootstrap-table 原来bootstrap还有这么强大的表格插件

    http://issues.wenzhixin.net.cn/bootstrap-table/index.html

  3. 使用HIBERNATE的SQL查询并将结果集自动转换成POJO

    在某些场合下,我们可能想使用HIBERNATE的框架提供的SQL查询接口,但是,由于实体没有做映射,HIBERNATE不能把结果集转换成你想要的List<POJO>,本文讨论如何在这种情况 ...

  4. Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)

    访问项目地址:http://192.168.254.100/ecshop 某个商品的 URL:http://192.168.254.100/ecshop/goods.php?id=3 现在需要实现把以 ...

  5. 判断返回数据是否为 null

    判断是否为null //尖括号                if ([regeocode.city isEqual:[NSNull class]]) {                    NSL ...

  6. 让你的PHP更安全之PHP.ini

    让你的PHP更安全之PHP.ini 发布时间:2013-05-02 12:43:06   来源:PHP100论坛   评论:0 点击: 次 [字号:大 中 小] QQ空间新浪微博腾讯微博人人网豆瓣网百 ...

  7. LR中的C语言问题

    今天在调试LR的脚本(C)时遇到了几个甚是头痛得问题,下面简单总结下: 1.首先LR中的C编译器遵循C90标准,规定在一个函数中,变量定义必须放在所有的执行语句之前!一旦在运行语句之间再有定义的话,会 ...

  8. mac终端中显示tree的命令

    寻觅了良久终于找到了mac下如何在终端显示tree的命令了,作为从linux下转过来的人,还没适应mac的finder,还是喜欢在命令行下查看文件. 命令: find . -print | sed - ...

  9. html EL表达式抬头

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <c:forE ...

  10. PureBasic 打开一个一行有多个数据的文件并读取其中某个数据

    如果有一个文件如下: TITLE = "Water Wurface Elevation"VARIABLES = "X", "Y", &quo ...