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 8035e5a

Browse files
committed
Feat: 326
1 parent 6a1186a commit 8035e5a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/326-power-of-three.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,35 @@
1313
## 代码
1414

1515
```php
16+
1617
class Solution {
1718

1819
/**
1920
* @param Integer $n
2021
* @return Boolean
2122
*/
2223
function isPowerOfThree($n) {
24+
// 方法1,递归
25+
if ($n <= 0) {
26+
return false;
27+
}
28+
if ($n == 1) {
29+
return true;
30+
}
31+
if (($n % 3) !== 0) {
32+
return false;
33+
}
34+
return $this->isPowerOfThree($n/3);
35+
/*
36+
// 方法2:循环取余
37+
while ($n && $n % 3 == 0) {
38+
$n = $n / 3;
39+
}
40+
return $n == 1;
41+
*/
42+
43+
/*
44+
// 方法3:乘法
2345
$a = 1;
2446
while($a <= $n){
2547
if ($a == $n) {
@@ -28,6 +50,9 @@ class Solution {
2850
$a *= 3;
2951
}
3052
return false;
53+
*/
54+
3155
}
3256
}
57+
3358
```

0 commit comments

Comments
(0)

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