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 ...
随机推荐
- FTPUtil工具类
package com.xxx.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...
- linux 下 打包 和解压缩
01-.tar格式解包:[*******]$ tar xvf FileName.tar打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩! ...
- ArcGIS 教程:Workflow Manager 高速浏览
应用程序概述 Workflow Manager 用户界面提供了用于在整个作业的生命周期中创建和管理作业的工具. 下面全部信息将会在本帮助文档的兴许章节中进行具体的说明. 文件菜单 新建 - 在系统中创 ...
- Jafka源码分析——LogManager
在Kafka中,LogManager负责管理broker上全部的Log(每个topic-partition为一个Log). 通过阅读源码可知其详细完毕的功能例如以下: 1. 依照预设规则对消息队列进行 ...
- MariaDB -- 数据类型
Mariadb 的数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:d ...
- Java后端发出post请求带参数并接收返回的json
核心代码: 参数格式: “key1=value1&key2=value2” /*** sendUrl (远程请求的URL)* param (远程请求参数)* JSONObject ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 树形压位DP
题目链接:http://codeforces.com/contest/766/problem/E Examples input 3 1 2 3 1 2 2 3 out 10 题意: 给你一棵n个点 ...
- ZOJ 3609 Modular Inverse(扩展欧几里德)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712 The modular modular multiplicat ...
- SQLyog软件里无法插入中文(即由默认的latin1改成UTF8编码格式)
问题详情: 无法插入中文? 解决办法: 需要修改编码格式,由默认的latin1改为utf8. 改成, 成功!
- override (C# Reference)
https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx The override modifier is required to extend o ...