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 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的数量的更多相关文章
- 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 ...
- (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 ...
- 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 ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 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 ...
- 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 ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- [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 ...
随机推荐
- bzoj 3671 随机数生成器 —— 暴力
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3671 原来256M是可以开两个3e7的数组的: 因为答案只有 n+m-1 个数,所以暴力判断 ...
- c++11中用_sntprintf代替_stprintf
sprintf.swprintf 分别是对单字节/双字节字符格式化的,wsprintf根据预定义指示符的不同可以对单字节/双字节字符格式化. wsprintf和swprintf比较,其实这两个函数对用 ...
- from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import Select Select(d.find_element_by_id(u'key_开户行')).first_sele ...
- BarTender SDK 实现调用模板条码打印
Demo:MyZebraPrint 基于BatTender .Net SDK 实现调用模板进行条码打印 有需要的朋友可以拿去研究下 在已经安装了BatTender10.1的电脑里测试通过. 下载地址: ...
- C#图片压缩类winform
using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Dr ...
- JVM类加载(2)—连接
2.连接 连接就是将已经加载到内存中的类的二进制数据合并到Java虚拟机的运行时环境中去,加载阶段尚未完成,连接阶段可能已经开始.连接阶段包含验证.准备.解析过程. 2.1.验证 验证.class文件 ...
- Ok6410裸机驱动学习(二)ARM基础知识
1.ARM工作模式 ARM微处理器支持7种工作模式,分别为: l 用户模式(usr):ARM处理器正常的程序执行状态(Linux用户态程序) l 快速中断模式(fiq):用于高速数据传输或通道处理 ...
- 【总结整理】JQuery基础学习---样式篇
进入官方网站获取最新的版本 http://jquery.com/download/ 中文 https://www.jquery123.com/ <!--JQuery:轻量级的JavaScr ...
- zookeeper相关知识的总结:
一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...
- JSP注释
------------------siwuxie095 在 JSP 文件中可以使用 HTML 注释 HTML 注释使用 < ...