Skip to content

Commit d86e6f9

Browse files
committed
feat: add solutions to lc problem: No.0326
No.0326.Power of Three
1 parent 9b6cfd7 commit d86e6f9

File tree

12 files changed

+244
-76
lines changed

12 files changed

+244
-76
lines changed

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,38 +167,34 @@ class Solution {
167167
```cpp
168168
class Solution {
169169
public:
170-
vector<int> p;
171-
172170
int countComponents(int n, vector<vector<int>>& edges) {
173-
p.resize(n);
171+
vector<int> p(n);
172+
iota(p.begin(), p.end(), 0);
174173
for (int i = 0; i < n; ++i) p[i] = i;
174+
function<int(int)> find = [&](int x) -> int {
175+
if (p[x] != x) p[x] = find(p[x]);
176+
return p[x];
177+
};
175178
for (auto& e : edges) {
176179
int a = e[0], b = e[1];
177180
p[find(a)] = find(b);
178181
}
179182
int ans = 0;
180-
for (int i = 0; i < n; ++i)
181-
if (i == find(i))
182-
++ans;
183+
for (int i = 0; i < n; ++i) ans += i == find(i);
183184
return ans;
184185
}
185-
186-
int find(int x) {
187-
if (p[x] != x) p[x] = find(p[x]);
188-
return p[x];
189-
}
190186
};
191187
```
192188
193189
### **Go**
194190
195191
```go
196-
func countComponents(n int, edges [][]int) int {
192+
func countComponents(n int, edges [][]int) (ans int) {
197193
p := make([]int, n)
198194
for i := range p {
199195
p[i] = i
200196
}
201-
var find func(x int) int
197+
var find func(int) int
202198
find = func(x int) int {
203199
if p[x] != x {
204200
p[x] = find(p[x])
@@ -209,13 +205,12 @@ func countComponents(n int, edges [][]int) int {
209205
a, b := e[0], e[1]
210206
p[find(a)] = find(b)
211207
}
212-
ans := 0
213208
for i := 0; i < n; i++ {
214209
if i == find(i) {
215210
ans++
216211
}
217212
}
218-
return ans
213+
return
219214
}
220215
```
221216

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,38 +93,34 @@ class Solution {
9393
```cpp
9494
class Solution {
9595
public:
96-
vector<int> p;
97-
9896
int countComponents(int n, vector<vector<int>>& edges) {
99-
p.resize(n);
97+
vector<int> p(n);
98+
iota(p.begin(), p.end(), 0);
10099
for (int i = 0; i < n; ++i) p[i] = i;
100+
function<int(int)> find = [&](int x) -> int {
101+
if (p[x] != x) p[x] = find(p[x]);
102+
return p[x];
103+
};
101104
for (auto& e : edges) {
102105
int a = e[0], b = e[1];
103106
p[find(a)] = find(b);
104107
}
105108
int ans = 0;
106-
for (int i = 0; i < n; ++i)
107-
if (i == find(i))
108-
++ans;
109+
for (int i = 0; i < n; ++i) ans += i == find(i);
109110
return ans;
110111
}
111-
112-
int find(int x) {
113-
if (p[x] != x) p[x] = find(p[x]);
114-
return p[x];
115-
}
116112
};
117113
```
118114
119115
### **Go**
120116
121117
```go
122-
func countComponents(n int, edges [][]int) int {
118+
func countComponents(n int, edges [][]int) (ans int) {
123119
p := make([]int, n)
124120
for i := range p {
125121
p[i] = i
126122
}
127-
var find func(x int) int
123+
var find func(int) int
128124
find = func(x int) int {
129125
if p[x] != x {
130126
p[x] = find(p[x])
@@ -135,13 +131,12 @@ func countComponents(n int, edges [][]int) int {
135131
a, b := e[0], e[1]
136132
p[find(a)] = find(b)
137133
}
138-
ans := 0
139134
for i := 0; i < n; i++ {
140135
if i == find(i) {
141136
ans++
142137
}
143138
}
144-
return ans
139+
return
145140
}
146141
```
147142

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution {
22
public:
3-
vector<int> p;
4-
53
int countComponents(int n, vector<vector<int>>& edges) {
6-
p.resize(n);
4+
vector<int> p(n);
5+
iota(p.begin(), p.end(), 0);
76
for (int i = 0; i < n; ++i) p[i] = i;
7+
function<int(int)> find = [&](int x) -> int {
8+
if (p[x] != x) p[x] = find(p[x]);
9+
return p[x];
10+
};
811
for (auto& e : edges) {
912
int a = e[0], b = e[1];
1013
p[find(a)] = find(b);
1114
}
1215
int ans = 0;
13-
for (int i = 0; i < n; ++i)
14-
if (i == find(i))
15-
++ans;
16+
for (int i = 0; i < n; ++i) ans += i == find(i);
1617
return ans;
1718
}
18-
19-
int find(int x) {
20-
if (p[x] != x) p[x] = find(p[x]);
21-
return p[x];
22-
}
2319
};
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
func countComponents(n int, edges [][]int) int {
1+
func countComponents(n int, edges [][]int) (ans int) {
22
p := make([]int, n)
33
for i := range p {
44
p[i] = i
55
}
6-
var find func(x int) int
6+
var find func(int) int
77
find = func(x int) int {
88
if p[x] != x {
99
p[x] = find(p[x])
@@ -14,11 +14,10 @@ func countComponents(n int, edges [][]int) int {
1414
a, b := e[0], e[1]
1515
p[find(a)] = find(b)
1616
}
17-
ans := 0
1817
for i := 0; i < n; i++ {
1918
if i == find(i) {
2019
ans++
2120
}
2221
}
23-
return ans
22+
return
2423
}

solution/0300-0399/0326.Power of Three/README.md

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,132 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:试除法**
60+
61+
如果 $n \gt 2$,我们可以不断地将 $n$ 除以 $3$,如果不能整除,说明 $n$ 不是 $3$ 的幂,否则继续除以 $3$,直到 $n$ 小于等于 $2$。如果 $n$ 等于 $1$,说明 $n$ 是 $3$ 的幂,否则不是 $3$ 的幂。
62+
63+
时间复杂度 $O(\log_3n)$,空间复杂度 $O(1)$。
64+
65+
**方法二:数学**
66+
67+
如果 $n$ 是 $3$ 的幂,那么 $n$ 最大是 $3^{19} = 1162261467$,因此我们只需要判断 $n$ 是否是 $3^{19}$ 的约数即可。
68+
69+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
70+
5971
<!-- tabs:start -->
6072

6173
### **Python3**
6274

6375
<!-- 这里可写当前语言的特殊实现逻辑 -->
6476

6577
```python
78+
class Solution:
79+
def isPowerOfThree(self, n: int) -> bool:
80+
while n > 2:
81+
if n % 3:
82+
return False
83+
n //= 3
84+
return n == 1
85+
```
6686

87+
```python
88+
class Solution:
89+
def isPowerOfThree(self, n: int) -> bool:
90+
return n > 0 and 1162261467 % n == 0
6791
```
6892

6993
### **Java**
7094

7195
<!-- 这里可写当前语言的特殊实现逻辑 -->
7296

7397
```java
98+
class Solution {
99+
public boolean isPowerOfThree(int n) {
100+
while (n > 2) {
101+
if (n % 3 != 0) {
102+
return false;
103+
}
104+
n /= 3;
105+
}
106+
return n == 1;
107+
}
108+
}
109+
```
110+
111+
```java
112+
class Solution {
113+
public boolean isPowerOfThree(int n) {
114+
return n > 0 && 1162261467 % n == 0;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
bool isPowerOfThree(int n) {
125+
while (n > 2) {
126+
if (n % 3) {
127+
return false;
128+
}
129+
n /= 3;
130+
}
131+
return n == 1;
132+
}
133+
};
134+
```
74135
136+
```cpp
137+
class Solution {
138+
public:
139+
bool isPowerOfThree(int n) {
140+
return n > 0 && 1162261467 % n == 0;
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func isPowerOfThree(n int) bool {
149+
for n > 2 {
150+
if n%3 != 0 {
151+
return false
152+
}
153+
n /= 3
154+
}
155+
return n == 1
156+
}
157+
```
158+
159+
```go
160+
func isPowerOfThree(n int) bool {
161+
return n > 0 && 1162261467%n == 0
162+
}
75163
```
76164

77165
### **TypeScript**
78166

79167
```ts
80168
function isPowerOfThree(n: number): boolean {
81-
while (n > 2) {
82-
if (n % 3) return false;
83-
n /= 3;
84-
}
85-
return n == 1;
169+
return n > 0 && 1162261467 % n == 0;
86170
}
87171
```
88172

173+
### **JavaScript**
174+
175+
```js
176+
/**
177+
* @param {number} n
178+
* @return {boolean}
179+
*/
180+
var isPowerOfThree = function (n) {
181+
return n > 0 && 1162261467 % n == 0;
182+
};
183+
```
184+
89185
### **...**
90186

91187
```

0 commit comments

Comments
 (0)