pull/13/head
chefyuan 2021-04-25 17:28:14 +08:00
parent a2ef361e13
commit 77f7acd452
1 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,206 @@
#
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
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 +
n = 13 ()
![](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....
********
> n = 1004 num = 10
0 ~ 9, 10 3 1 0 1 1 1 2 ~ 9
> ** n 100410141024 3 **
>
> ryan0414
### **n = 1004**
** 1004 ** 1
0 1
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/位数1.2x7xcbxtkjo0.png)
> * num 1
>
> 10 0 ~ 10 0 10 10
>
> 1 0 ~ 9 10 10 * 10
0010 ~ 0919 1
### n = 1014
** 1014 ** 1
1 1
1014 1 10 1014 10 ~ 1014 1
1014 1004 + 10 = 1014
10 ~ 1014 0010 ~ 0919 1004 1010 ~ 1014
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/次数为1十位.4e6s2zqwtsw0.png)
> * num + + 1 10 * 10 + 4 + 1
>
> 1
>
> * num + 1
>
>
3 4
### n = 1024
** 1024 ** 1
2 ~ 9 1 0010 1019
0010 ~ 09191010 ~ 1019
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/高位.1wn8di6g1t6.png)
> * num + num 10 * 10 + 10 1
>
> 1 10 1 0 ~ 9 10 (num)
1014 1
> 绿
>
> low = 0 cur = n % 10, num = 1, count = 0, high = n / 10;
![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;
//这里我们可以提出 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;
num *= 10;
}
return count;
}
}
```
: O(logn) O(1)