给定一个整数 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的个数的更多相关文章

  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 ...

  2. 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 ...

  3. (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 ...

  4. 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 ...

  5. 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 ...

  6. 233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  7. 【LeetCode】233. Number of Digit One

    题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...

  8. 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 ...

  9. 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. VS调试STL问题总结

    ---恢复内容开始--- 以前写代码总觉用自己写的东西比较牛逼,vector?stack?为什么不自己实现.后来才认识到这是个幼稚的想法!首先每次都自己实现是一种重复劳动:其次,自己写的话很难保证没有 ...

  2. Arctic Network POJ - 2349

    The Department of National Defence (DND) wishes to connect several northern outposts by a wireless n ...

  3. Java开发笔记(九十九)定时器与定时任务

    前面介绍了线程的几种运行方式,不管哪种方式,一旦调用了线程实例的start方法,都会立即启动线程的事务处理.然而某些业务场景在事务执行时间方面有特殊需求,例如期望延迟若干时间之后才开始事务运行,又如期 ...

  4. MongoDB C#驱动

    烟波钓徒 MongoDB C#驱动 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial 笔记 首先下载驱动.驱动有两个文件 Mongo ...

  5. 踩坑录-libreoffice fatal error com.sun.start.ucb.Interactive.AugmentedIOException: a folder could not be created

    错误概要: 1.LibreOffice可以正常使用: 2.启动tomcat报错如下: Fatal error The application cannot be started. ][context= ...

  6. Elasticsearch学习系列之单模式下API的增删改查操作

    这里我们通过Elasticsearch的marvel插件实现单模式下API的增删改查操作 索引的初始化操作 创建索引之前可以对索引进行初始化操作,比如先指定shard数量以及replicas的数量 代 ...

  7. C#之快速排序 C#之插入排序 C#之选择排序 C#之冒泡排序

    C#之快速排序   算法描述 1.假定数组首位元素为“枢轴”,设定数列首位(begin)与末位(end)索引: 2.由末位索引对应元素与“枢轴”进行比较,如果末位索引对应元素大于“枢轴”元素,对末位索 ...

  8. DBCP,C3P0,Tomcat_JDBC 性能及稳定性測试

    原创文章,转载请指明出处:http://aub.iteye.com/blog/1404219, 尊重他人即尊重自己 DBCP,C3P0,Tomcat_JDBC 性能及稳定性測试 1.測试环境: 硬件环 ...

  9. 怎样处理Gradle中的这个文件下载慢的问题的

    如图:在build.gradle中的dependencies中加上要依赖的包后,就点击sync gradle.然后就开始了下载.在此过程中我是FQ了的(在此同时我是可以用chrome进入https:/ ...

  10. 在mac上安装gradle(超详细,直接按步骤操作即可轻松搞定)

    第一步, 就是先download最新版本的gradle,网址如下: http://gradle.org/gradle-download/ 然后将下载下来的zip包放解压到本地任意的路径上, 例如,我本 ...