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 ...
随机推荐
- Linux下汇编语言学习笔记26 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- iOS 数据库操作崩溃提示“ int rc = sqlite3_step([_statement statement]);”或者提示“ rc = sqlite3_step(pStmt);”
数据库崩溃崩溃提示“ int rc = sqlite3_step([_statement statement]);”或者提示“ rc = sqlite3_step(pStmt);”的时候,可 ...
- Educational Codeforces Round 45 (Rated for Div. 2) C、D
C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...
- CSU - 1333 1333: Funny Car Racing(spfa)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333 这题多了一个限制条件是每一条路都会规律的开放a时间关闭b时间,车子必须在开放的时候进入,在关 ...
- Ubuntu 16.04 GNOME无法使用拼音输入法问题
说明:添加好之后重启!不然会出现输入错乱的问题. 参考: http://forum.ubuntu.org.cn/viewtopic.php?t=477765
- POJ3977 Subset 折半枚举
题目大意是给定N个数的集合,从这个集合中找到一个非空子集,使得该子集元素和的绝对值最小.假设有多个答案,输出元素个数最少的那个. N最多为35,假设直接枚举显然是不行的. 可是假设我们将这些数分成两半 ...
- Unity3D 玻璃 Shader
Shader "Custom/Glass" { // Upgrade NOTE: replaced 'SeperateSpecular' with 'SeparateSpec ...
- 【智能路由器】让MT7620固件openwrt支持USB
[智能路由器]系列文章连接 http://blog.csdn.net/u012819339/article/category/5803489 首先确定硬件有USB,这个得检查板子是否引出了usb口,一 ...
- (十)Net Core项目使用Cookies (八)Net Core项目使用Controller之三-入参
(十)Net Core项目使用Cookies 一.简介 1.Net Core可以直接使用Cookies,但是调用方式有些区别. 2.Net Core将Request和Response分开实现. 二.基 ...
- 动态生成页面(一)——ASP.NET中Literal使用
在页面中加入内容时,假设是静态内容.无需使用容器,能够直接将标记作为HTML直接加入到页面中:可是,假设是动态内容,则必须借助容器将内容加入到页面中.典型的容器有:Label控件.Literal控件. ...