Skip to content

Commit 03deb73

Browse files
committed
Merge branch 'master' of https://github.com/illuz/leetcode
2 parents 081a05f + 9bcd786 commit 03deb73

File tree

7 files changed

+120
-55
lines changed

7 files changed

+120
-55
lines changed

readme.md

Lines changed: 55 additions & 55 deletions
Large diffs are not rendered by default.

solutions/292.Nim_Game/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
1515
可以去了解下,入门:[博弈入门:从数学游戏开始](http://www.guokr.com/article/500/),进阶的就去看 [Nim 的 Wiki](https://en.wikipedia.org/wiki/Nim) 吧。
1616

17+
这题目可能有很多人看过,不过要讲清楚可能并不轻松。虽然解很简单,不过却是一道考察表达能力的好题。
18+
1719

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_math_1.cpp
4+
* Create Date: 2016-02-23 01:18:19
5+
* Descripton: i
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
#include <cmath>
10+
11+
using namespace std;
12+
const int N = 0;
13+
14+
class Solution {
15+
public:
16+
bool isPowerOfThree(int n) {
17+
return fmod(log10(n)/log10(3), 1)==0;
18+
}
19+
};
20+
21+
int main() {
22+
23+
return 0;
24+
}
25+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Solution {
2+
public boolean isPowerOfThree(int n) {
3+
while (n >= 3) {
4+
if (n % 3 != 0) return false;
5+
n /= 3;
6+
}
7+
return n == 1;
8+
}
9+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def isPowerOfThree(n):
2+
return n in [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907, 43046721, 129140163, 387420489, 1162261467, 3486784401]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Solution {
2+
public boolean isPowerOfThree(int n) {
3+
// 1162261467 is 3^19, 3^20 is bigger than int
4+
return ( n>0 && 1162261467%n==0);
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## 326. Power of Three (Easy)
2+
3+
4+
5+
### **链接**
6+
7+
题目:https://leetcode.com/problems/power-of-three/
8+
代码(github):https://github.com/illuz/leetcode
9+
10+
### **题意**
11+
12+
判断一个数是否是 3 的次方数。
13+
14+
### **分析**
15+
16+
1. 最普通的做法:用 3 一次一次除,能除尽就是了。
17+
2. 用 log 去做,中学数学题,`x=log3(y)=(log2(y)/log2(3))`,然后判断 x 是不是整数就行了。(考察点:浮点数的计算)
18+
3. tricky 的做法,因为参数是 int 类型的,所以 3 的次方就那么几个,先全算出来。
19+
4. 也是 tricky 的做法,直接判断能不能被 1162261467 整除,1162261467 是 int 内最大的 (3^x) 数,也就是说它的因子都是 3,能被它整除的数就一定也是 (3^x) 数。
20+
21+

0 commit comments

Comments
 (0)