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 than or equal to n.
For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
Hint:
- Beware of overflow.
class Solution {
public:
int countDigitOne(int n) {
if(n <= )
return ;
vector<int> v;
int t = n;
while(t)
{
v.push_back(t%);
t /= ;
}
int l = v.size(), rest = n - v[l-] * pow(, l-);
return (v[l-] > ? pow(, l-) : rest + ) + v[l-] * (l-) * pow(, l-) + countDigitOne(rest);
}
};
233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数的更多相关文章
- 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 ...
- 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 ...
- 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 某一范围内的整数包含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 ...
- 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 ...
随机推荐
- vi及缩进设置
set autoindent,把当前行的对起格式应用到下一行: set smartindent,智能的选择对起方式: set tabstop=4,设置tab键为4个空格: set shiftwidth ...
- [HDOJ5773]The All-purpose Zero(贪心,DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意:给n个数,其中0可以用任何数字代替,问如何替换0使整个数列中的LIS最长. 0可以用任何数 ...
- shell脚本批量ping测试IP是否通
#!/bin/bash rm -f result.txt cat ip.txt | fping > result.txt 2行代码就搞定,很方便,初学shell,很强大,问了下同事,但是shel ...
- jquery动态样式操作
获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: 1 var p_class = $(" ...
- FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)
FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术) Time Limit: 1000MS Memory Limit: 257792K [Description ...
- 详解.NET异步
在说到异步前,先来理一下几个容易混淆的概念,并行.多线程.异步. 并行,一般指并行计算,是说同一时刻有多条指令同时被执行,这些指令可能执行于同一CPU的多核上,或者多个CPU上,或者多个物理主机甚至多 ...
- Log4j XML 配置
Xml代码 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configurat ...
- 表单美化-原生javascript和jQuery多选按钮(兼容IE6)
前些天我们讲了下单选按钮的美化今天来做表单元素多选按钮的美化.我们的想法是:利用多选按钮是否被选中和是否不给选择的特性来为按钮的父元素添加对应的样式,就是说用什么的样式是由按钮的状态来决定. 用到的图 ...
- iOS - Photo Album 图片/相册管理
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...
- iOS - UIStoryboard
前言 NS_CLASS_AVAILABLE_IOS(5_0) @interface UIStoryboard : NSObject @available(iOS 5.0, *) public clas ...