[LeetCode] Number of Digit One

简介: The following idea is taken from a book named 《剑指offer》 published in China. Suppose n = 271, it then breaks [1, 271] into [1, 71] and [72, 271].

The following idea is taken from a book named 《剑指offer》 published in China.

Suppose n = 271, it then breaks [1, 271] into [1, 71] and [72, 271]. For [72, 271], the number of 1 on the hundreds are 10^2 = 100 (note that if the digit on the higest bit is 1, then the number of 1's on that bit will be 72, which is 271 % 100 + 1). To compute the number of 1on tens and units, we further break [72, 271] into [72, 171] and [172, 271]. Each interval has 10^1 = 10 1's on both the tens and units. So the overall number of 1's on tens and units is2 * 2 * 10^1 = 40. Now we are only left with [1, 71], which can be solved recursively. Theint n is transformed into a string num to facilitate some operations.

The code is as follows.

 1 class Solution {
 2 public:
 3     int countDigitOne(int n) {
 4         if (n <= 0) return 0;
 5         string num = to_string(n);
 6         return countOne(num);
 7     }
 8 private:
 9     int countOne(string num) {
10         if (num.empty()) return 0;
11         int first = num[0] - '0';
12         if (num.length() == 1) {
13             if (first) return 1;
14             return 0;
15         }
16         int highestBit;
17         if (first > 1) highestBit = powerTen(num.length() - 1);
18         else if (first == 1) highestBit = stoi(num.substr(1)) + 1; 
19         int otherBits = first * (num.length() - 1) * powerTen(num.length() - 2);
20         int recurCount = countOne(num.substr(1));
21         return highestBit + otherBits + recurCount;
22     }
23     int powerTen(int exp) {
24         int p = 1;
25         for (int i = 0; i < exp; i++)
26             p *= 10;
27         return p;
28     }
29 };

This link has a brilliant 4-line solution! The code is rewritten as follows.

1 class Solution {
2 public:
3     int countDigitOne(int n) {
4         int counts = 0;
5         for (int m = 1000000000; m; m /= 10)
6             counts += (n / m + 8) / 10 * m + (n / m % 10 == 1) * (n % m + 1);
7         return counts;
8     }
9 };

 

目录
相关文章
|
11月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
95 1
|
4月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
5月前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
41 0
|
11月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
44 0
|
11月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
38 0
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行