From 31d8824b9c66b6b08e835e763a9644f13f4fbfe6 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Thu, 31 Mar 2022 10:50:26 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20728?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\344\272\214\345\210\206.md" | 1 + .../\345\223\210\345\270\214\350\241\250.md" | 1 + "Index/\346\211\223\350\241\250.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 160 ++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 "LeetCode/721-730/728. \350\207\252\351\231\244\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\344\272\214\345\210\206.md" "b/Index/\344\272\214\345\210\206.md" index 41002fdc..7b476c01 100644 --- "a/Index/\344\272\214\345\210\206.md" +++ "b/Index/\344\272\214\345\210\206.md" @@ -28,6 +28,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | +| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\345\223\210\345\270\214\350\241\250.md" "b/Index/\345\223\210\345\270\214\350\241\250.md" index d1ed79aa..f90d2f84 100644 --- "a/Index/\345\223\210\345\270\214\350\241\250.md" +++ "b/Index/\345\223\210\345\270\214\350\241\250.md" @@ -35,6 +35,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩🤩 | +| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\346\211\223\350\241\250.md" "b/Index/\346\211\223\350\241\250.md" index a5b9f5b7..d0031ee3 100644 --- "a/Index/\346\211\223\350\241\250.md" +++ "b/Index/\346\211\223\350\241\250.md" @@ -3,6 +3,7 @@ | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/721-730/728. \350\207\252\351\231\244\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/721-730/728. \350\207\252\351\231\244\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..14f9a68e --- /dev/null +++ "b/LeetCode/721-730/728. \350\207\252\351\231\244\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,160 @@ +### 题目描述 + +这是 LeetCode 上的 **[728. 自除数](https://leetcode-cn.com/problems/self-dividing-numbers/solution/by-ac_oier-pvb1/)** ,难度为 **简单**。 + +Tag : 「模拟」、「打表」、「哈希表」、「二分」 + + + +自除数是指可以被它包含的每一位数整除的数。 + +* 例如,$128$ 是一个 自除数 ,因为 `128 % 1 == 0`,`128 % 2 == 0`,`128 % 8 == 0`。 + +自除数不允许包含 $0$ 。 + +给定两个整数 `left` 和 `right` ,返回一个列表,列表的元素是范围 $[left, right]$ 内所有的 自除数 。 + +示例 1: +``` +输入:left = 1, right = 22 + +输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] +``` +示例 2: +``` +输入:left = 47, right = 85 + +输出:[48,55,66,77] +``` + +提示: +* $1 <= left <= right <= 10^4$ + +--- + +### 模拟 + +根据题意进行模拟即可。 + +代码: +```Java +class Solution { + public List selfDividingNumbers(int left, int right) { + List ans = new ArrayList<>(); + out:for (int i = left; i <= right; i++) { + int cur = i; + while (cur != 0) { + int t = cur % 10; + if (t == 0 || i % t != 0) continue out; + cur /= 10; + } + ans.add(i); + } + return ans; + } +} +``` + + +```Python +class Solution: + def selfDividingNumbers(self, left: int, right: int) -> List[int]: + ans = [] + for i in range(left, right + 1): + cur, ok = i, True + while cur and ok: + ok = not ((t := cur % 10) == 0 or i % t != 0) + cur //= 10 + if ok: + ans.append(i) + return ans +``` +* 时间复杂度:令 $n = right - left + 1$,复杂度为 $O(n \log{right})$ +* 空间复杂度:$O(1)$ + +--- + +### 打表 + 二分 + +利用数据范围只有 $1e4$,我们可以打表预处理出所有的自除数,通过二分找到 $[left, right]$ 范围内的最小自除数,再从前往后找到所有合法的自除数。 + +代码: +```Java +class Solution { + static List list = new ArrayList<>(); + static { + out:for (int i = 1; i <= 10000; i++) { + int cur = i; + while (cur != 0) { + int u = cur % 10; + if (u == 0 || i % u != 0) continue out; + cur /= 10; + } + list.add(i); + } + } + public List selfDividingNumbers(int left, int right) { + List ans = new ArrayList<>(); + int l = 0, r = list.size() - 1; + while (l < r) { + int mid = l + r >> 1; + if (list.get(mid) >= left) r = mid; + else l = mid + 1; + } + while (r < list.size() && list.get(r) <= right) ans.add(list.get(r++)); + return ans; + } +} +``` +* 时间复杂度:令 $n = right - left + 1$,复杂度为 $O(\log{C} + n)$,其中 $C = 1e4$ +* 空间复杂度:$O(C)$ + +--- + +### 打表 + 哈希表 + +由于我们打表预处理的做法空间复杂度上界已经是 $O(C)$,所有我们可以干脆将索引也预处理出来,从而避免二分操作。 + +其中 $hash[x]$ 的含义为值不超过 $x$ 的最大自除数在 `list` 中的下标。 + +代码: +```Java +class Solution { + static List list = new ArrayList<>(); + static int[] hash = new int[100010]; + static { + for (int i = 1; i <= 10000; i++) { + int cur = i; + boolean ok = true; + while (cur != 0 && ok) { + int u = cur % 10; + if (u == 0 || i % u != 0) ok = false; + cur /= 10; + } + if (ok) list.add(i); + hash[i] = list.size() - 1; + } + } + public List selfDividingNumbers(int left, int right) { + List ans = new ArrayList<>(); + int idx = list.get(hash[left]) == left ? hash[left] : hash[left] + 1; + while (idx < list.size() && list.get(idx) <= right) ans.add(list.get(idx++)); + return ans; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(C)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.726` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +