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 ...
随机推荐
- BZOJ3401:[USACO2009MAR]Look Up
浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...
- NSSet 用法
//集合NSSet //集合也是储存对象,和字典一样也是无序存储 //集合中里面的元素不能重复 //集合里面的元素可以随机取出 //用数组放到集合中 NSArray *a10 = @[p1 , p2 ...
- 上海-北京间通过Azure Storage的RA-GRS类型的存储账户进行快速复制
Azure的Blob存储分成多种类型,目前主要有: 其中RA-GRS可以在上海-北京两个数据中心间同步数据.并且,在第二个数据中心可以只读的方式读取这个存储账户中的Blob内容. 虽然GRS采用的是准 ...
- 关于 vs 2012 键盘无法输入的问题
使用vs2012 新建了一个类文件之后,vs2012的编辑界面突然出现奇怪的问题,键盘无法输入! 最后调查的结果是由于resharper插件导致的. 可以将插件禁用然后启用. 也可以删除resharp ...
- TreeSet 和TreeMap 排序
TreeSet 有两种排序方式 1. Java.lang.Comparble+compareTo new TreeSet() 用这种方法必须要求实体类实现Comparable接口,也就是说要求添加到T ...
- kvm ip查看
1.virsh --list(查看有哪些服务器) 2.virsh dumpxml 虚拟机名称 查看服务器对应的mac地址 3.然后再宿主机上arp -a 查看对应的mac地址对应的ip
- OpenXml 2.0 读取Excel
Excel 单元格中的数据类型包括7种: Boolean.Date.Error.InlineString.Number.SharedString.String 读取源代码: List<strin ...
- layui 工具条实现分页
1.页面 <div id="getShowTable" style="width: 100%; height: auto;clear: both;"> ...
- 通过命令查看jboss的错误日志文件
链接jboss: ssh ip -l jboss
- 6.7 使用IDEA导入工程
打开IDEA->File->new -> Project from existing ..->选择你的工程,导入: 请注意,在130或者40上面的项目并不是最新的,sunny也 ...