Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Solution] Power of four #343

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Math/PowerFour.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Question Link: https://leetcode.com/problems/power-of-four/
* Primary idea: n must be a power of 2 and count of zero bits before the (only) set bit is even.
* Time Complexity: O(logn), Space Complexity: O(1)
*/

class PowerOfFour {
func isPowerOfFour(_ n: Int) -> Bool {
guard n > 0 else {
return false
}

let isPowerOfTwo = (n & (n - 1) == 0)

guard isPowerOfTwo else {
return false
}

var numberOfZeros = 0
var n = n

while n != 1 {
n = n >> 1
numberOfZeros += 1
}

return numberOfZeros % 2 == 0
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 323 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 324 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -330,6 +330,7 @@
[Pow(x, n)](https://leetcode.com/problems/isomorphic-strings/)| [Swift](./Math/Pow.swift)| Medium| O(logn)| O(1)|
[Power of Two](https://leetcode.com/problems/power-of-two/)| [Swift](./Math/PowerTwo.swift)| Easy| O(1)| O(1)|
[Power of Three](https://leetcode.com/problems/power-of-three/)| [Swift](./Math/PowerThree.swift)| Easy| O(1)| O(1)|
[Power of Four](https://leetcode.com/problems/power-of-four/)| [Swift](./Math/PowerFour.swift)| Easy| O(logn)| O(1)|
[Super Power](https://leetcode.com/problems/super-pow/)| [Swift](./Math/SuperPow.swift)| Medium| O(n)| O(1)|
[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)| [Swift](./Math/SumTwoIntegers.swift)| Easy| O(n)| O(1)|
[Reverse Integer](https://leetcode.com/problems/reverse-integer/)| [Swift](./Math/ReverseInteger.swift)| Easy| O(n)| O(1)|
Expand Down Expand Up @@ -578,7 +579,7 @@
| [Swift](./String/ReverseVowelsOfAString.swift) | 345 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | Easy
| [Swift](./String/ReverseString.swift) | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | Easy
| [Swift](./Math/IntegerBreak.swift) | 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | Medium
| | 342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | Easy
| [Swift](./Math/PowerFour.swift) | 342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | Easy
| [Swift](./Design/FlattenNestedListIterator.swift) | 341 | [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/) | Medium
| [Swift](./String/LongestSubstringMostKDistinctCharacters.swift) | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) ♥ | Hard
| [Swift](./DP/NestedListWeightSum.swift) | 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) ♥ | Easy
Expand Down