Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31d8824

Browse files
✨feat: Add 728
1 parent b8807e0 commit 31d8824

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

‎Index/二分.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
| [540. 有序数组中的单一元素](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/solution/gong-shui-san-xie-er-duan-xing-fen-xi-yu-17nv/) | 中等 | 🤩🤩🤩🤩 |
2929
| [611. 有效三角形的个数](https://leetcode-cn.com/problems/valid-triangle-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-triangle-number/solution/gong-shui-san-xie-yi-ti-san-jie-jian-dan-y1we/) | 中等 | 🤩🤩🤩🤩 |
3030
| [704. 二分查找](https://leetcode-cn.com/problems/binary-search/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-search/solution/gong-shui-san-xie-yun-yong-er-fen-zhao-f-5jyj/) | 简单 | 🤩🤩🤩🤩🤩 |
31+
| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 |
3132
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
3233
| [786. 第 K 个最小的素数分数](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-8ymk/) | 中等 | 🤩🤩🤩 |
3334
| [852. 山脉数组的峰顶索引](https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/solution/gong-shui-san-xie-er-fen-san-fen-cha-zhi-5gfv/) | 简单 | 🤩🤩🤩🤩🤩 |

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
| [705. 设计哈希集合](https://leetcode-cn.com/problems/design-hashset/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-hashset/solution/yi-ti-san-jie-jian-dan-shu-zu-lian-biao-nj3dg/) | 简单 | 🤩🤩🤩🤩 |
3636
| [706. 设计哈希映射](https://leetcode-cn.com/problems/design-hashmap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-hashmap/solution/yi-ti-shuang-jie-jian-dan-shu-zu-lian-bi-yhiw/) | 简单 | 🤩🤩🤩🤩 |
3737
| [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-atoms/solution/gong-shui-san-xie-shi-yong-xiao-ji-qiao-l5ak4/) | 困难 | 🤩🤩🤩🤩 |
38+
| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 |
3839
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
3940
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
4041
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩🤩 |

‎Index/打表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| [326. 3的幂](https://leetcode-cn.com/problems/power-of-three/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-three/solution/gong-shui-san-xie-yi-ti-san-jie-shu-xue-8oiip/) | 简单 | 🤩🤩🤩 |
44
| [401. 二进制手表](https://leetcode-cn.com/problems/binary-watch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-watch/solution/gong-shui-san-xie-jian-dan-ti-xue-da-bia-gwn2/) | 简单 | 🤩🤩🤩🤩🤩 |
55
| [650. 只有两个键的键盘](https://leetcode-cn.com/problems/2-keys-keyboard/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/2-keys-keyboard/solution/gong-shui-san-xie-yi-ti-san-jie-dong-tai-f035/) | 中等 | 🤩🤩🤩🤩 |
6+
| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 |
67
| [1137. 第 N 个泰波那契数](https://leetcode-cn.com/problems/n-th-tribonacci-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/n-th-tribonacci-number/solution/gong-shui-san-xie-yi-ti-si-jie-die-dai-d-m1ie/) | 简单 | 🤩🤩🤩🤩 |
78
| [1414. 和为 K 的最少斐波那契数字数目](https://leetcode-cn.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-rgty8/) | 中等 | 🤩🤩🤩🤩 |
89
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「打表」、「哈希表」、「二分」
6+
7+
8+
9+
自除数是指可以被它包含的每一位数整除的数。
10+
11+
* 例如,128ドル$ 是一个 自除数 ,因为 `128 % 1 == 0`,`128 % 2 == 0`,`128 % 8 == 0`
12+
13+
自除数不允许包含 0ドル$ 。
14+
15+
给定两个整数 `left``right` ,返回一个列表,列表的元素是范围 $[left, right]$ 内所有的 自除数 。
16+
17+
示例 1:
18+
```
19+
输入:left = 1, right = 22
20+
21+
输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
22+
```
23+
示例 2:
24+
```
25+
输入:left = 47, right = 85
26+
27+
输出:[48,55,66,77]
28+
```
29+
30+
提示:
31+
* 1ドル <= left <= right <= 10^4$
32+
33+
---
34+
35+
### 模拟
36+
37+
根据题意进行模拟即可。
38+
39+
代码:
40+
```Java
41+
class Solution {
42+
public List<Integer> selfDividingNumbers(int left, int right) {
43+
List<Integer> ans = new ArrayList<>();
44+
out:for (int i = left; i <= right; i++) {
45+
int cur = i;
46+
while (cur != 0) {
47+
int t = cur % 10;
48+
if (t == 0 || i % t != 0) continue out;
49+
cur /= 10;
50+
}
51+
ans.add(i);
52+
}
53+
return ans;
54+
}
55+
}
56+
```
57+
58+
59+
```Python
60+
class Solution:
61+
def selfDividingNumbers(self, left: int, right: int) -> List[int]:
62+
ans = []
63+
for i in range(left, right + 1):
64+
cur, ok = i, True
65+
while cur and ok:
66+
ok = not ((t := cur % 10) == 0 or i % t != 0)
67+
cur //= 10
68+
if ok:
69+
ans.append(i)
70+
return ans
71+
```
72+
* 时间复杂度:令 $n = right - left + 1,ドル复杂度为 $O(n \log{right})$
73+
* 空间复杂度:$O(1)$
74+
75+
---
76+
77+
### 打表 + 二分
78+
79+
利用数据范围只有 1ドルe4,ドル我们可以打表预处理出所有的自除数,通过二分找到 $[left, right]$ 范围内的最小自除数,再从前往后找到所有合法的自除数。
80+
81+
代码:
82+
```Java
83+
class Solution {
84+
static List<Integer> list = new ArrayList<>();
85+
static {
86+
out:for (int i = 1; i <= 10000; i++) {
87+
int cur = i;
88+
while (cur != 0) {
89+
int u = cur % 10;
90+
if (u == 0 || i % u != 0) continue out;
91+
cur /= 10;
92+
}
93+
list.add(i);
94+
}
95+
}
96+
public List<Integer> selfDividingNumbers(int left, int right) {
97+
List<Integer> ans = new ArrayList<>();
98+
int l = 0, r = list.size() - 1;
99+
while (l < r) {
100+
int mid = l + r >> 1;
101+
if (list.get(mid) >= left) r = mid;
102+
else l = mid + 1;
103+
}
104+
while (r < list.size() && list.get(r) <= right) ans.add(list.get(r++));
105+
return ans;
106+
}
107+
}
108+
```
109+
* 时间复杂度:令 $n = right - left + 1,ドル复杂度为 $O(\log{C} + n),ドル其中 $C = 1e4$
110+
* 空间复杂度:$O(C)$
111+
112+
---
113+
114+
### 打表 + 哈希表
115+
116+
由于我们打表预处理的做法空间复杂度上界已经是 $O(C),ドル所有我们可以干脆将索引也预处理出来,从而避免二分操作。
117+
118+
其中 $hash[x]$ 的含义为值不超过 $x$ 的最大自除数在 `list` 中的下标。
119+
120+
代码:
121+
```Java
122+
class Solution {
123+
static List<Integer> list = new ArrayList<>();
124+
static int[] hash = new int[100010];
125+
static {
126+
for (int i = 1; i <= 10000; i++) {
127+
int cur = i;
128+
boolean ok = true;
129+
while (cur != 0 && ok) {
130+
int u = cur % 10;
131+
if (u == 0 || i % u != 0) ok = false;
132+
cur /= 10;
133+
}
134+
if (ok) list.add(i);
135+
hash[i] = list.size() - 1;
136+
}
137+
}
138+
public List<Integer> selfDividingNumbers(int left, int right) {
139+
List<Integer> ans = new ArrayList<>();
140+
int idx = list.get(hash[left]) == left ? hash[left] : hash[left] + 1;
141+
while (idx < list.size() && list.get(idx) <= right) ans.add(list.get(idx++));
142+
return ans;
143+
}
144+
}
145+
```
146+
* 时间复杂度:$O(n)$
147+
* 空间复杂度:$O(C)$
148+
149+
---
150+
151+
### 最后
152+
153+
这是我们「刷穿 LeetCode」系列文章的第 `No.726` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
154+
155+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
156+
157+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
158+
159+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
160+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /