File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Question Link: https://leetcode.com/problems/power-of-three/
3+ * Primary idea: Use the largest 3^n int number to mod
4+ * Time Complexity: O(1), Space Complexity: O(1)
5+ *
6+ */
7+
8+ class PowerThree {
9+ func isPowerOfThree( n: Int ) -> Bool {
10+ guard n > 0 else {
11+ return false
12+ }
13+
14+ return 1162261467 % n == 0
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Question Link: https://leetcode.com/problems/power-of-two/
3+ * Primary idea: Use and to solve the problem
4+ * Time Complexity: O(n), Space Complexity: O(1)
5+ *
6+ */
7+
8+ class PowerTwo {
9+ func isPowerOfTwo( n: Int ) -> Bool {
10+ guard n > 0 else {
11+ return false
12+ }
13+
14+ return n & ( n - 1 ) == 0
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments