algorithm-base/animation-simulation/剑指offer/1的个数.md

251 lines
9.9 KiB
Java
Raw Normal View History

2021-04-25 09:28:14 +00:00
#
2021-07-23 15:44:19 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
2021-04-25 09:28:14 +00:00
>
>
>
2021-07-20 13:13:10 +00:00
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-04-25 09:28:14 +00:00
AC
## leetcode 233. 1
****
n n 1
1
> n = 13
> 6
2
> n = 0
> 0
** n 1 **
1
1 +1
n 1
n = abcd
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/1的次数1.1s5l5k3qy3y8.png)
1 1 1
** n ** 1 + 1 +
2021-07-23 15:44:19 +00:00
n = 13 ()
2021-04-25 09:28:14 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/1的次数2.1horkktykr7k.png)
13 1
1 2 1 4
2 + 4 = 6
> 11 2 1
>
> 1 1
1
n
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/1的次数3.21nr01qnlz40.png)
1
num num = 1 10 100....
********
2021-07-20 13:13:10 +00:00
> n = 1004 num = 10
2021-04-25 09:28:14 +00:00
0 ~ 9, 10 3 1 0 1 1 1 2 ~ 9
> ** n 100410141024 3 **
>
> ryan0414
2021-07-23 15:44:19 +00:00
### **n = 1004**
2021-04-25 09:28:14 +00:00
** 1004 ** 1
0 1
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/位数1.2x7xcbxtkjo0.png)
2021-07-23 15:44:19 +00:00
> \* num 1
2021-04-25 09:28:14 +00:00
>
> 10 0 ~ 10 0 10 10
>
2021-07-23 15:44:19 +00:00
> 1 0 ~ 9 10 10 \* 10
2021-04-25 09:28:14 +00:00
2021-07-23 15:44:19 +00:00
0010 ~ 0919 1
2021-04-25 09:28:14 +00:00
### n = 1014
** 1014 ** 1
1 1
2021-07-23 15:44:19 +00:00
1014 1 10 1014 10 ~ 1014 1
2021-04-25 09:28:14 +00:00
1014 1004 + 10 = 1014
2021-07-23 15:44:19 +00:00
10 ~ 1014 0010 ~ 0919 1004 1010 ~ 1014
2021-04-25 09:28:14 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/次数为1十位.4e6s2zqwtsw0.png)
2021-07-23 15:44:19 +00:00
> _ num + + 1 10 _ 10 + 4 + 1
2021-04-25 09:28:14 +00:00
>
> 1
>
2021-07-23 15:44:19 +00:00
> \* num + 1
2021-04-25 09:28:14 +00:00
>
>
3 4
2021-07-20 13:13:10 +00:00
### n = 1024
2021-04-25 09:28:14 +00:00
** 1024 ** 1
2 ~ 9 1 0010 1019
0010 ~ 09191010 ~ 1019
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/高位.1wn8di6g1t6.png)
2021-07-23 15:44:19 +00:00
> _ num + num 10 _ 10 + 10 1
2021-04-25 09:28:14 +00:00
>
> 1 10 1 0 ~ 9 10 (num)
1014 1
> 绿
>
2021-07-23 15:44:19 +00:00
> low = 0 cur = n % 10, num = 1, count = 0, high = n / 10;
2021-04-25 09:28:14 +00:00
![1](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/1的个数.5yccejufzc80.gif)
****
```java
class Solution {
public int countDigitOne(int n) {
//高位
int high = n;
//低位
int low = 0;
//当前位
int cur = 0;
int count = 0;
int num = 1;
while (high != 0 || cur != 0) {
cur = high % 10;
high /= 10;
2021-07-20 13:13:10 +00:00
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
2021-04-25 09:28:14 +00:00
if (cur == 0) count += high * num;
2021-07-20 13:13:10 +00:00
else if (cur == 1) count += high * num + 1 + low;
2021-04-25 09:28:14 +00:00
else count += (high + 1) * num;
//低位
2021-07-20 13:13:10 +00:00
low = cur * num + low;
2021-04-25 09:28:14 +00:00
num *= 10;
}
return count;
}
}
```
2021-07-19 15:02:05 +00:00
Swift Code:
```swift
class Solution {
func countDigitOne(_ n: Int) -> Int {
var high = n, low = 0, cur = 0, count = 0, num = 1
while high != 0 || cur != 0 {
cur = high % 10
high /= 10
2021-07-20 13:13:10 +00:00
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
2021-07-19 15:02:05 +00:00
if cur == 0 {
count += high * num
2021-07-20 13:13:10 +00:00
} else if cur == 1 {
2021-07-19 15:02:05 +00:00
count += high * num + 1 + low
} else {
count += (high + 1) * num
}
low = cur * num + low
num *= 10
}
return count
}
}
```
2021-07-23 15:44:19 +00:00
: O(logn) O(1)
2021-04-25 09:28:14 +00:00
2021-07-20 13:13:10 +00:00
C++ Code:
```C++
class Solution
{
public:
int countDigitOne(int n)
{
// 高位, 低位, 当前位
int high = n, low = 0, cur = 0;
int count = 0, num = 1;
//数字是0的时候完全没必要继续计算
while (high != 0)
{
cur = high % 10;
high /= 10;
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
if (cur == 0)
count += (high * num);
else if (cur == 1)
count += (high * num + 1 + low);
else
count += ((high + 1) * num);
//低位
low = cur * num + low;
//提前检查剩余数字, 以免溢出
if (high != 0)
num *= 10;
}
return count;
}
};
2021-07-23 15:44:19 +00:00
```