forked from NilaakashSingh/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Math] add Solution to Power of Two and Power of Three
- Loading branch information
Yi Gu
committed
Jul 3, 2016
1 parent
f53cd5b
commit cb80ec0
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/power-of-three/ | ||
* Primary idea: Use the largest 3^n int number to mod | ||
* Time Complexity: O(1), Space Complexity: O(1) | ||
* | ||
*/ | ||
|
||
class PowerThree { | ||
func isPowerOfThree(n: Int) -> Bool { | ||
guard n > 0 else { | ||
return false | ||
} | ||
|
||
return 1162261467 % n == 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/power-of-two/ | ||
* Primary idea: Use and to solve the problem | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
* | ||
*/ | ||
|
||
class PowerTwo { | ||
func isPowerOfTwo(n: Int) -> Bool { | ||
guard n > 0 else { | ||
return false | ||
} | ||
|
||
return n & (n - 1) == 0 | ||
} | ||
} |