Number of Digit One(Medium)
1.算法说明:
如3141592,在m(digitDivide)=100时,即要求计算百位上“1”的个数
其中a为31415,b为92,31415中出现了3142次“1”,因为每10个数里面出现1次“1”。而实际上,31415是3141500,所以把31415中1的个数再乘以m。如3141400~3141499中,前缀为31414的数出现了100次,所以需要乘以m(此时是100)。
class Solution {
public:
int countDigitOne(int n) {
int ans=0;
for(long long digitDivide=1;digitDivide<=n;digitDivide*=10)
{
int a=n/digitDivide;
int b=n%digitDivide;
ans+=(a+8)/10*digitDivide+(a%10==1)*(b+1);
}
return ans;
}};
Number of Digit One(Medium)的更多相关文章
- (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] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [Leetcode] Number of Digit Ones
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 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 ...
- 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
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- [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 ...
随机推荐
- mint linux的几个问题
刚开机登录时, 键盘不停的输入同一个字符 发现在虚拟机里安装的系统没有问题, 因此怀疑配置有冲突, 新建一个用户果然能解决问题 解决方法: 把 主目录下, 隐藏的配置目录删除, 重新登录后再配置. 4 ...
- java8 String intern()
public class Solution { public static void main(String[] args) { String a = new String("he" ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 序列使用
MySQL 序列是一组整数:1, 2, 3, ...,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现. 使用 AUTO_INCREMENT ...
- MySQL各种存储引擎对比总结
1.MyISAM 是MySQL5.5版之前默认的存储引擎,特点:数据文件和索引文件可以放置在不同的目录,平局分布IO,获得更快的速度.不支持事务,不支持外键. 2.InnoDB 是 MySQL5.5版 ...
- dp高难本攻略
903. DI 序列的有效排列 public int numPermsDISequence(String S) { int n=S.length(); int [][]dp=new int [n+1] ...
- 109.大型的csv文件的处理方式
HttpResponse对象将会将响应的数据作为一个整体返回,此时如果数据量非常大的话,长时间浏览器没有得到服务器的响应,就会超过默认的超时时间,返回超时.而StreamingHttpResponse ...
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- spring学习之spring入门
一 spring的基础 1:什么是spring spring是由Rod Johnson组织和开发的一个分层 的Java SE/EE 一站式轻量级开源框架,它以Ioc(控制反转)和 AOP(面向切面编程 ...
- ccf201403-3 记录一个神tmwa了的代码 莫非我没看懂题。。。
#include <string.h> #include<cstdio> #include<stdio.h> #include <iostream> # ...
- 创建Maven项目时Maven中的GroupID和ArtifactID的意思
GroupID 是项目组织中唯一的标识符,对应Java包结构,在项目中看到的是main目录里java的目录结构. ArtifactID是项目的唯一的标识符,实际对应项目的名称(就是idea中工程的名字 ...