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 ...
随机推荐
- csu - 1537: Miscalculation (模拟题)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1537 因为给出的式子是必定合法的,只要用两个栈分别保存符号和数字.算出答案后和从左至右算的答案比对 ...
- 搬砖--杭电校赛(dfs)
搬砖 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submissi ...
- js 计算获取鼠标相对某个点的移动旋转角度
// 旋转角度 function getAngle(cen, first, second) { // cen : 中心点 [0,0] // first : 开始点 [1,3] // second : ...
- list.ensureCapacity竟然会变慢
list.ensureCapacity竟然会变慢 jdk1.8 应该是做了优化了: public class Test10 { public static void main(String[] arg ...
- Tree Operations 打印出有向图中的环
题目: You are given a binary tree with unique integer values on each node. However, the child pointers ...
- mysql数据库批量高速插入
近期在处理一批数据,须要从库中表里的字段进行处理然后导出到一个新表中.只是这个表的数据量有近500w条. 这数据量出现的的问题是须要处理的时间好长. 首先想到,一句一句的插入,大数据量处理时间好长,忽 ...
- Linux/Android——input子系统核心 (三)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/42123673 之前的博客有涉及到linux的input子系统,这里学习记录一下input模块. ...
- 7-80 HTML5新增的JS选择器
7-80 HTML5新增的JS选择器 学习要点 HTML5新增的JS选择器 在传统的 JavaScript 开发中,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 ta ...
- MongoDB全文搜索——目前尚不支持针对特定field的搜索
> db.articles.createIndex( { subject: "text" } ) { "createdCollectionAutomatically ...
- [JSOI 2016] 最佳团体
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4753 [算法] 很明显的分数规划 可以用树形动态规划(树形背包)检验答案 时间复杂度 ...