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 ...
随机推荐
- python一个正则表达式的不解
htmlSource="data-lazy=\"http://gtms01.alicdn.com/tps/i1/T1faOCFQXXXXc2jIrl-.png\"&quo ...
- Java哪些集合类是线程安全的?
早在jdk的1.1版本中,所有的集合都是线程安全的.但是在1.2以及之后的版本中就出现了一些线程不安全的集合,为什么版本升级会出现一些线程不安全的集合呢?因为线程不安全的集合普遍比线程安全的集合效率高 ...
- Mac电脑如何彻底删除node
之前本来想搭建一个hexo来写博客的,但是最后还是放弃,老老实实就在博客园和CSDN写博文了,这里记录一下怎么在Mac电脑下彻底删除node.js的方法 下面这个方法是我结合了网上好几个方法综合在一起 ...
- SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:6-1 函数
6-1 各种各样的函数 ● 函数的种类很多,无需全都记住,只需要记住具有代表性的函数就可以了,其他的可以在使用时再进行查询. ■函数的种类所谓函数,就是输入某一值得到相应输出结果的功能,输入值称为参数 ...
- SQL基础教程(第2版)第1章 数据库和SQL:练习题
CREATE TABLE Addressbook ( regist_no INTEGER NOT NULL, name ) NOT NULL, address ) NOT NULL, tel_no ) ...
- 51nod A 魔法部落(逆元费马小定理)
A 魔法部落 小Biu所在的部落是一个魔法部落,部落中一共有n+1个人,小Biu是魔法部落中最菜的,所以他的魔力值为1,魔法部落中n个人的魔法值都不相同,第一个人的魔法值是小Biu的3倍,第二个人的魔 ...
- Python D9 学习
Python 设置环境 当安装好Python 后 在计算机的属性里面 高级语言设置 环境变量. 环境变量里面的path 更改为Python的 树目录 可以从计算机直接下达命令 打开Pytho ...
- vi_终端中的编辑器操作
vi -- 终端中的编辑器 目标 vi 简介 打开和新建文件 三种工作模式 常用命令 分屏命令 常用命令速查图 01. vi 简介 1.1 学习 vi 的目的 在工作中,要对 服务器 上的文件进行 简 ...
- WINSCP 使用笔记
前期准备: 1.官网下载:http://winscp.net/eng/docs/lang:chs 官网C#示例:http://winscp.net/eng/docs/library#csharp 当然 ...
- 计蒜客 数独(DFS)
蒜头君今天突然开始还念童年了,想回忆回忆童年.他记得自己小时候,有一个很火的游戏叫做数独.便开始来了一局紧张而又刺激的高阶数独.蒜头君做完发现没有正解,不知道对不对? 不知道聪明的你能否给出一个标准答 ...