原题链接在这里: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. ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机

    ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机 使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linu ...

  2. easyui datagrid分页要点总结

    easyui的datagird插件比较好用,也很方便.网上也有很多热的网友贴出了使用代码,但是很少有网友指出在使用过程应该注意的地方,让我实在搞不清分页应该怎么使用.我就说下使用分页功能中要注意的一个 ...

  3. PHPUnit在Windows下的配置及使用

    由于我们项目涉及到php,因此需要对php代码进行单元测试.经过一番了解,决定用PHPUnit来测试php.PHPUnit花了不少时间摸索如何配置PHPUnit,看官网的文档也是一把泪.但知道怎么配置 ...

  4. hdu Turn the corner

    这题是道三分的题,首先要分析满足条件的情况,这个就是平面几何的功夫了.要想车子能够转弯成功,最上面那个点到水平线的距离要小于等于y.这里h和s的公式就是利用平面几何的知识求出来的:s=l*cos(a) ...

  5. 【转】【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式

    本文属转载,来源: http://www.byywee.com/page/M0/S868/868615.html 关于Model验证官方资料: http://msdn.microsoft.com/zh ...

  6. [转]Visual Studio 实用扩展推荐

    本文转自 http://www.cnblogs.com/stg609/p/3726898.html Visual Studio 拥有非常不错的可扩展性,在之前的文章中,我也给大家示范了如何进行编辑器的 ...

  7. Powershell连接Office 365各组件的方法

    参考: http://www.exchangecn.com/office365/20150108_540.html 1. 适用于 IT 专业人员 RTW 的 Microsoft Online Serv ...

  8. 匈牙利命名法,骆驼命名法(camel),帕斯卡(Pascal)命名法(转)

    一.匈牙利命名法      Windows 编程中用到的变量(还包括宏)的命名规则匈牙利命名法,这种命名技术是由一位能干的 Microsoft 程序员查尔斯·西蒙尼(Charles Simonyi) ...

  9. web_custom_request函数详解

    在LR中当使用HTML录制方式时,录制的脚本中主要由函数web_link().web_submit_form().web_url().web_submit_data()组成,当使用HTTP录制方式时, ...

  10. MySQL 5.6.3

    SHOW VARIABLES LIKE '%version%'; https://dev.mysql.com/doc/refman/5.6/en/explain.html As of MySQL 5. ...