Skip to content

Commit dd6da3f

Browse files
Merge pull request SharingSource#149 from SharingSource/ac_oier
✨feat: Add 326
2 parents 70be7e1 + c003f43 commit dd6da3f

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

Index/打表.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [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/) | 简单 | 🤩🤩🤩 |
34
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
45
| [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/) | 中等 | 🤩🤩🤩🤩 |
56
| [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/) | 简单 | 🤩🤩🤩🤩 |

Index/数学.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [233. 数字 1 的个数](https://leetcode-cn.com/problems/number-of-digit-one/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-digit-one/solution/gong-shui-san-xie-jiang-shu-wei-dp-wen-t-c9oi/) | 困难 | 🤩🤩🤩🤩 |
1414
| [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ugly-number/solution/gong-shui-san-xie-jian-dan-de-fen-qing-k-dlvg/) | 简单 | 🤩🤩 |
1515
| [313. 超级丑数](https://leetcode-cn.com/problems/super-ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/super-ugly-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-jyow/) | 中等 | 🤩🤩🤩 |
16+
| [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/) | 简单 | 🤩🤩🤩 |
1617
| [342. 4的幂](https://leetcode-cn.com/problems/power-of-four/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-four/solution/gong-shui-san-xie-zhuan-hua-wei-2-de-mi-y21lq/) | 简单 | 🤩🤩🤩 |
1718
| [446. 等差数列划分 II - 子序列](https://leetcode-cn.com/problems/arithmetic-slices-ii-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices-ii-subsequence/solution/gong-shui-san-xie-xiang-jie-ru-he-fen-xi-ykvk/) | 困难 | 🤩🤩🤩🤩🤩 |
1819
| [470. 用 Rand7() 实现 Rand10()](https://leetcode-cn.com/problems/implement-rand10-using-rand7/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/implement-rand10-using-rand7/solution/gong-shui-san-xie-k-jin-zhi-zhu-wei-shen-zmd4/) | 中等 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[326. 3的幂](https://leetcode-cn.com/problems/power-of-three/solution/gong-shui-san-xie-yi-ti-san-jie-shu-xue-8oiip/)** ,难度为 **简单**
4+
5+
Tag : 「数学」、「打表」
6+
7+
给定一个整数,写一个函数来判断它是否是 $3$ 的幂次方。如果是,返回 $true$ ;否则,返回 $false$ 。
8+
9+
整数 $n$ 是 $3$ 的幂次方需满足:存在整数 $x$ 使得 $n == 3^x$
10+
11+
示例 1:
12+
```
13+
输入:n = 27
14+
15+
输出:true
16+
```
17+
示例 2:
18+
```
19+
输入:n = 0
20+
21+
输出:false
22+
```
23+
示例 3:
24+
```
25+
输入:n = 9
26+
27+
输出:true
28+
```
29+
示例 4:
30+
```
31+
输入:n = 45
32+
33+
输出:false
34+
```
35+
36+
提示:
37+
* -231 <= n <= 231 - 1
38+
39+
---
40+
41+
### 数学
42+
43+
一个不能再朴素的做法是将 $n$ 对 $3$ 进行试除,直到 $n$ 不再与 $3$ 呈倍数关系,最后判断 $n$ 是否为 $3^0 = 1$ 即可。
44+
45+
代码:
46+
```Java
47+
class Solution {
48+
public boolean isPowerOfThree(int n) {
49+
if (n <= 0) return false;
50+
while (n % 3 == 0) n /= 3;
51+
return n == 1;
52+
}
53+
}
54+
```
55+
* 时间复杂度:$O(\log_{3}n)$
56+
* 空间复杂度:$O(1)$
57+
58+
---
59+
60+
### 倍数 & 约数
61+
62+
题目要求不能使用循环或递归来做,而传参 $n$ 的数据类型为 `int`,这引导我们首先分析出 `int` 范围内的最大 $3$ 次幂是多少,约为 $3^{19} = 1162261467$。
63+
64+
如果 $n$ 为 $3$ 的幂的话,那么必然满足 $n * 3^k = 1162261467$,即 $n$ 与 $1162261467$ 存在倍数关系。
65+
66+
因此,我们只需要判断 $n$ 是否为 $1162261467$ 的约数即可。
67+
68+
代码:
69+
```Java
70+
class Solution {
71+
public boolean isPowerOfThree(int n) {
72+
return n > 0 && 1162261467 % n == 0;
73+
}
74+
}
75+
```
76+
* 时间复杂度:$O(1)$
77+
* 空间复杂度:$O(1)$
78+
79+
80+
---
81+
82+
### 打表
83+
84+
另外一个更容易想到的「不使用循环/递归」的做法是进行打表预处理。
85+
86+
使用 `static` 代码块,预处理出不超过 `int` 数据范围的所有 $3$ 的幂,这样我们在跑测试样例时,就不需要使用「循环/递归」来实现逻辑,可直接 $O(1)$ 查表返回。
87+
88+
代码:
89+
```Java
90+
class Solution {
91+
static Set<Integer> set = new HashSet<>();
92+
static {
93+
int cur = 1;
94+
set.add(cur);
95+
while (cur <= Integer.MAX_VALUE / 3) {
96+
cur *= 3;
97+
set.add(cur);
98+
}
99+
}
100+
public boolean isPowerOfThree(int n) {
101+
return n > 0 && set.contains(n);
102+
}
103+
}
104+
```
105+
* 时间复杂度:将打表逻辑交给 $OJ$ 执行的话,复杂度为 $O(\log_3{C})$,$C$ 固定为 $2147483647$;将打表逻辑放到本地执行,复杂度为 $O(1)$
106+
* 空间复杂度:$O(n)$
107+
108+
---
109+
110+
### 最后
111+
112+
这是我们「刷穿 LeetCode」系列文章的第 `No.326` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
113+
114+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
115+
116+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
117+
118+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
119+

0 commit comments

Comments
 (0)