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 - 1)
这个递推公式可以这么观察得到:
i = 0, X[0] = 0
i = 1, 从1到9, X[1] = 1
i = 2, 从1到99, X[2] = 20:可以设想,把所有数都写成两位数(比如1写成01, 2写成02),我们暂且不统计最高位的1, 则首先至少有10 * X[1]个1,然后我们考虑最高位的1,它只存在于11,12...19,也是10个
i = 3,从1到999,X[3] = ? : 同样先不统计最高位的1,首先至少有10 * X[2]个1,再统计最高位,应该还要加上100(10^(3-1))
逐步思考,通项公式就出来了:X[i] = 10 * X[i - 1] + 10^(i - 1)
记我们要求解的函数为f(x)
现在我们考虑一般的N,假设它表示成10进制有n位:a[n]a[n-1]...a[1]
用C[i]表示f(a[i]a[i-1]...a[1])的结果
递推过程仍旧类似,但此时每一位的上限有了限制,不能自由的从0到9取值
a[i] = 0时,C[i] = C[i - 1]
a[i] = 1时:C[i] = X[i - 1] + C[i - 1] + a[i-1]a[i-1]...a[1] + 1
a[i] > 1时:C[i] = a[i] * X[i - 1] + 10 ^(i - 1) + C[i - 1]
class Solution {
public:
int countDigitOne(int n) {
int base = 10;
vector<int> digits;
while (n >= 1) {
digits.push_back(n % base);
n /= base;
}
vector<int> nums;
int s = 0;
base = 1;
for (int i = 0; i < digits.size(); i++) {
s += base * digits[i];
nums.push_back(s);
base *= 10;
}
vector<int> C(digits.size() + 1, 0);
vector<int> X(digits.size() + 1, 0);
base = 1;
for (int i = 1; i < C.size(); i++) {
X[i] = 10 * X[i - 1] + base;
if (digits[i - 1] == 0) {
C[i] = C[i - 1];
}
else if (digits[i - 1] == 1) {
C[i] = X[i - 1] + C[i - 1] + 1 + (i > 1 ? nums[i - 2] : 0);
}
else {
C[i] = digits[i - 1] * X[i - 1] + C[i - 1] + base;
}
base *= 10;
}
return C[C.size() - 1];
}
};
leetcode 233 Number of Digit One的更多相关文章
- 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 某一范围内的整数包含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
题目: 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题解汇总(1021~1031)
bzoj1021:普通dp bzoj1022:裸的Anti-Nim 必胜:①sg=0且所有不超过1 ②sg>1且存在至少一个超过1 bzoj1023:http://www.cnblogs.com ...
- svg矢量图绘制以及转换为Android可用的VectorDrawable资源
项目需要 要在快速设置面板里显示一个VoWiFi图标(为了能够区分出来图形,我把透明的背景填充为黑色了) 由于普通图片放大后容易失真,这里我们最好用矢量图(SVG(Scalable Vector Gr ...
- 实验二Step1-有序顺序表
#include<stdio.h> struct job { ];//作业名称 char status;//当前状态 int arrtime;//到达时间 int reqtime;//要求 ...
- Uva----------(11078)Open Credit System
Open Credit System Input:Standard Input Output: Standard Output In an open credit system, the studen ...
- RestSharp .net 轻量级rest客户端
RestSharp Simple REST and HTTP API Client for .NET 官网:http://restsharp.org/ GiHub: https://github.co ...
- ECC中的CRM UI端摆弄
前段时间想搞CRM了,可是公司没有环境,就去ECC直接试试事务码,结果竟然可以打开网页...兴奋之余又去看了一下CRM里的一些CLASS,结果很多都是没有的.沮丧! 后来想想,只能用UI的框架,挂WD ...
- eclipse 中提示tomcat 的端口被占用了 后的最快捷解决方法
很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就重新配置tomcat 或者去修改tomcat的端口号 ,其实这么做太麻 ...
- 实战SQL Server 2005镜像配置全过程
SQL Server 2005镜像配置基本概念 我理解的SQL Server 2005镜像配置实际上就是由三个服务器(也可以是同一服务器的三个 SQL 实例)组成的一个保证数据的环境,分别是:主服务器 ...
- c++ 普通高精除高精
//codevs3118 高精度练习之除法 //打出了高精除高精,内心有点小激动. //还记得已开始学的时候非常难打 #include<cstdio>#include<cstring ...
- C# Lodop实现打印
项目的Debug文件夹下有个template文件夹,里面有用到的js.自己建的要打印的网页和用到的背景图 1.打印方法: class print { public void printzb(strin ...