233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数。
例如:
给定 n = 13,
返回 6,因为数字1出现在下数中出现:1,10,11,12,13。
详见:https://leetcode.com/problems/number-of-digit-one/description/
Java实现:
方法一:
class Solution {
public int countDigitOne(int n) {
StringBuilder sb=new StringBuilder();
for(int i=1;i<=n;++i){
sb.append(i);
}
int cnt=0;
String str=sb.toString();
for(int i=0;i<str.length();++i){
if(str.charAt(i)=='1'){
++cnt;
}
}
return cnt;
}
}
方法二:
class Solution {
public int countDigitOne(int n) {
int cnt=0;
for(long m=1;m<=n;m*=10){
long a=n/m,b=n%m;
if(a%10==0){
cnt+=a/10*m;
}else if(a%10==1){
cnt+=a/10*m+(b+1);
}else{
cnt+=(a/10+1)*m;
}
}
return cnt;
}
}
C++实现:
方法一:
class Solution {
public:
int countDigitOne(int n) {
int cnt=0;
for(long long m=1;m<=n;m*=10)
{
int a=n/m,b=n%m;
if(a%10==0)
{
cnt+=a/10*m;
}
else if(a%10==1)
{
cnt+=a/10*m+(b+1);
}
else
{
cnt+=(a/10+1)*m;
}
}
return cnt;
}
};
方法二:
class Solution {
public:
int countDigitOne(int n) {
int cnt=0;
for(long long m=1;m<=n;m*=10)
{
cnt+=(n/m+8)/10*m+(n/m%10==1)*(n%m+1);
}
return cnt;
}
};
233 Number of Digit One 数字1的个数的更多相关文章
- [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 ...
- 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 ...
- (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 ...
- 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 ...
- 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
题目: 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 ...
- 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 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 ...
随机推荐
- HDU 5644 King's Pliot【费用流】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 题意: 每天都有p[i]个飞行员进行阅兵,飞行员只工作一天. m个休假公式,花费tt[i]元让 ...
- Delphi简单的数据操作类
unit MyClass; uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, VCL ...
- 一个1x1px大小Data/Base64数据的gif透明图片
<img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNg ...
- [bzoj2982]combination_卢卡斯
Combination bzoj-2982 题目大意:求$C_n^m/%10007$. 注释:$1\le n,m\le 2\cdot 10^9$. 想法:裸卢卡斯定理. 先处理出$mod$数之内的阶乘 ...
- Sum-(最大子序列和)
http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=652&pid=1003 题目大意: 给你一个序列,你随便找一 ...
- 在springBoot与quartz 整合中 @Transaction 失效
问题1::springBoot在与quartz 整合时,使用@Transaction 注解时事务失效 解决方案:创建一个类使用@component被spring管理 ,使用@Transaction标识 ...
- 程序猿是如何解决SQLServer占CPU100%的--马非码
http://www.cnblogs.com/marvin/p/ASolutionForSQLServerCauseHighCPU.html
- Android API Guides –System Permissions
系统权限 声明: 本文由Gordon翻译 公布于www.dlvoice.com 欢迎转载,但请保留此声明 原文地址:http://developer.android.com/guide/topics/ ...
- Java中的Nested Classes和Inner Classes
Java中的Nested Classes和Inner Classes Java有嵌套类(Nested Classes)和内部类(Inner Classes)的概念. 嵌套类(Nested Classe ...
- 音乐播放器之myeclipse项目
音乐播放器: 这个音乐播放器是用myeclipse打开的项目.假设有问题记得改掉文件的路径名.还有假设图片不显示也可能是图片的路径名不正确,如音乐无法播放也可能是路径名不正确.总之这个游戏有文件的引用 ...