Skip to content

Commit

Permalink
[Math] add Solution to Power of Two and Power of Three
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Gu committed Jul 3, 2016
1 parent f53cd5b commit cb80ec0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Math/PowerThree.swift
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
}
}
16 changes: 16 additions & 0 deletions Math/PowerTwo.swift
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
}
}

0 comments on commit cb80ec0

Please sign in to comment.