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.

方法一

 class Solution {
public:
int countDigitOne(int n) {
int cnt=;
for(long long m=;m<=n;m*=)
{
int a=n/m,b=n%m;
if(a%==)
cnt+=a/*m;
else if(a%==)
cnt+=a/*m+(b+);
else
cnt+=(a/+)*m;
}
return cnt;
}
};

方法二

 class Solution {
public:
int countDigitOne(int n) {
int cnt=;
for(long long m=;m<=n;m*=)
cnt=cnt+(n/m+)/*m+(n/m%==)*(n%m+);
return cnt;
}
};

LeetCode 233 Number of Digit One 某一范围内的整数包含1的数量的更多相关文章

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

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

  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. 233. Number of Digit One

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

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

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

  8. 233 Number of Digit One 数字1的个数

    给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...

  9. [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 ...

随机推荐

  1. 实用的原生js图片轮播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Uva 213

    1. 问题 第一次发现新的存储方式,int code[8][1<<8]; 用于存储二进制的形式 将字符以是十进制的方式存储到数组中 如何消除 \n \r 的影响,进行多行的输入 2. 代码 ...

  3. jquery插件开发常用总结一

    由于使用jquery插件后当form表单提交的时候,若发生错误,同时有验证错误文本时,即使用rules和message后,会自动生成一个label标签里面装有错误文件值. 我们可以替换它: 方式为:v ...

  4. 树莓派 Learning 001 装机 ---之 1 安装NOOBS系统

    树莓派安装NOOBS系统 (使用的树莓派板卡型号:Raspberry Pi 2 Model B V1.1)(板卡的型号在板子正面的丝印层上印着,你可以看到.) RASPBERRY PI 2 MODEL ...

  5. HDFS内容追加

    配置:hdfs-site.xml <property> <name>dfs.support.append</name> <value>true</ ...

  6. ssh动态转发小记

    ssh,一般常用来做远程登录管理,也就是连上远程机器,得到一个shell,然后交互式地在上面敲命令-看结果-再敲命令. 偶尔也会用在脚本里,做些自动化批处理上传下载的操作,但本质上也是用shell来执 ...

  7. python笔记——均值、方差、中位数计算

    from __future__ import print_function # 均值计算 data = [3.53, 3.47, 3.51, 3.72, 3.43] average = float(s ...

  8. 第五课5、ROS客户端2

    1.简单的主题(topic)发布者和主题订阅者: 编写主题发布者节点需要: a.初始化ROS系统: b.广播消息:在foo主题上发布Foo_type_msg类型的消息 c.指定频率发布消息到foo主题 ...

  9. lsnrctl start错误Linux Error: 29: Illegal seek (翻译:非法谋取)

    现在,想不起来为什么ORACLE的监听,怎么就突然无法起来了呢. 好吧,问题反正就是发生了. lsnrctl start 遇到如下错误, LSNRCTL for Linux: Version 10.2 ...

  10. win10外接显示器时有些应用和里面的字体显示比较模糊

    打开系统设置 - 选择显示 - 选中外接的显示器 - 点击 "高级缩放设置" 进去后将 "允许 Windows  尝试修改应用,使其不模糊"打开,然后关闭应用再 ...