原题链接在这里: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. 基于Berkeley DB实现的持久化队列

    转自:http://guoyunsky.iteye.com/blog/1169912 队列很常见,但大部分的队列是将数据放入到内存.如果数据过多,就有内存溢出危险,而且长久占据着内存,也会影响性能.比 ...

  2. Redis内存存储结构分析

    1 Redis 内存存储结构 本文是基于 Redis-v2.2.4 版本进行分析. 1.1 Redis 内存存储总体结构 Redis 是支持多key-value数据库(表)的,并用 RedisDb 来 ...

  3. Centos下设置静态IP

    使用VirtualBox装了很多虚拟机,默认采用的DHCP的方式,但是随着虚拟机越来越多以后,这种方式很不方便,所以需要将机器的IP都设置为静态IP 需要添加的几项内容的解释:IPADDR    IP ...

  4. 【iCore2 模块相关资料】发布模块DEMO 代码包,目前支持 iM_TFT30、 iM_LAN和 iM_RGB 三个模块

    iCore2 模块底板 和部分模块发布了,所以我们做了一个 DEMO 代码包,此代码包现在有以下功能: 1.支持 iM_TFT30 3寸触摸液晶模块(硬件已发布): 2.支持 iM_LAN 100M以 ...

  5. YII2.0 secruity

    保存密码不能用明文保存,用MD5或者sha1哈希化是安全,但是随着硬件的发展,可能会暴力破解,目前能够对抗暴力破解的哈希算法是 bcrypt,Yii提供了两个帮助函数使用crypt进行安全的哈希加密 ...

  6. JavaWEB中读取配置信息

    第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ...

  7. java的Arrays工具类实战

    java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的.静态方法是属于类的,不是属于类的对象.所以可以直接使用类名加方法名进行调用.Arrays作为一个工具类,能很好的操作数组 ...

  8. MAC 环境 ionic build android 命令在"Downloading http://services.gradle.org/distributions/gradle-2.13-all.zip"卡住问题

    如题: 环境 node: 4.5.0,npm:2.15.9,cordova :6.3.1, ionic:2.1.0 在ionic build android 命令执行时,会去这个网址下载 gradel ...

  9. 从个人的角度谈谈本次GNTC大会的收获

    GNTC资料:from sdnlab 从个人的角度谈谈本次大会的收获 从本次大会的主题演讲来看,目前SDN.NFV的最前沿已经不再像五年前持观望态度以及探讨,各大运营商.各大厂商已经将SDN.NFV具 ...

  10. Nginx配置文件详细说明[转]

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等worker_process ...