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

For example:

Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

解题思路:

递归

static public int countDigitOne(int n) {
if (n == 0)
return 0;
if (n < 10)
return 1;
int length = 0;
int firstNum = n;
while (firstNum >= 10) {
firstNum /= 10;
length++;
}
int basic = (int) Math.pow(10, length);
if (firstNum > 1) {
int tmp1 = countDigitOne(basic - 1);
int tmp2 = basic;
int tmp3 = countDigitOne(n - basic * firstNum);
return firstNum * tmp1 + tmp2 + tmp3;
} else {
int tmp1 = countDigitOne(basic - 1);
int tmp2 = n + 1 - basic;
int tmp3 = countDigitOne(n - basic);
return tmp1 + tmp2 + tmp3;
} }

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

  1. (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 ...

  2. 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 ...

  3. leetcode 233 Number of Digit One

    这题属于需要找规律的题.先想一下最简单的情形:N = 10^n - 1 记X[i]表示从1到10^i - 1中 1 的个数,则有如下递推公式:X[i] = 10 * X[i - 1] + 10^(i ...

  4. 【LeetCode】233. Number of Digit One

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

  5. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  6. 233. Number of Digit One

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

  7. 233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数

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

  8. 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 ...

  9. Java for LeetCode 191 Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

随机推荐

  1. [c#]RabbitMQ的简单使用

    摘要 Message Queue消息队列,简称MQ,是一种应用程序对应用程序的通信方法,应用程序通过读写出入队列的消息来通信,而无需专用连接来链接它们.消息传递指的是程序之间通过在消息中发送数据进行通 ...

  2. linux下查找包含BOM头的文件和清除BOM头命令

    查找包含BOM头的文件,命令如下:   grep -r -I -l $'^\xEF\xBB\xBF' ./   这条命令会查找当前目录及子目录下所有包含BOM头的文件,并把文件名在屏幕上输出.   但 ...

  3. 密码学初级教程(三)公钥密码RSA

    密码学家工具箱中的6个重要的工具: 对称密码 公钥密码 单向散列函数 消息认证码 数字签名 伪随机数生成器 公钥密码(非对称密码) 问题: 公钥认证问题 处理速度是对称密码的几百分之一 求离散对数非常 ...

  4. 基于iSCSI的SQL Server 2012群集测试(三)--SQL Server 2012群集安装总结

    5.SQL Server 2012群集安装总结 5.1 群集与非群集的安装区别总结 SQL Server虚拟名称: 非群集环境下,本地服务器的名称就是SQL Server服务器名称:但在群集环境下,由 ...

  5. nyoj 10 skiing 搜索+动归

    整整两天了,都打不开网页,是不是我提交的次数太多了? nyoj 10: #include<stdio.h> #include<string.h> ][],b[][]; int ...

  6. HTML5 之Canvas 绘制时钟 Demo

    <!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...

  7. ZOJ 3201 Tree of Tree

    树形DP.... Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weig ...

  8. jquery选择器(一)-基础选择器

    1. ID元素选择器 $("#btn1") 2. class元素选择器 $(".btn") 3. 标签元素选择器 $("div") 4. 全 ...

  9. jekyll 安装过程

    如果有, linux以源码包方式发布, 方便,快捷, 容易出错,安装内容难找到,版本容易冲突.兼容性会出错.如何解决这种方式:1.上网查找答案,你遇到的别人也有,关键词匹配到,好像没有别的办法解决了, ...

  10. Redis学习笔记六:独立功能之 Lua 脚本

    Redis 2.6 开始支持 Lua 脚本,通过在服务器环境嵌入 Lua 环境,Redis 客户端中可以原子地执行多个 Redis 命令. 使用 eval 命令可以直接对输入的脚本求值: 127.0. ...