From 37f92ad64b3e403315d51b5f25eaedab6e2f2858 Mon Sep 17 00:00:00 2001 From: Sorokin Igor <34890417+srk1nn@users.noreply.github.com> Date: Fri, 9 Sep 2022 17:03:14 +0300 Subject: [PATCH 1/4] [Solution] add solution power of four --- Math/PowerOfFour.swift | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Math/PowerOfFour.swift diff --git a/Math/PowerOfFour.swift b/Math/PowerOfFour.swift new file mode 100644 index 0000000..50d4ffa --- /dev/null +++ b/Math/PowerOfFour.swift @@ -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 + } +} From 7973f1486fd7c2e6f0164f4579679d45322ada11 Mon Sep 17 00:00:00 2001 From: Sorokin Igor <34890417+srk1nn@users.noreply.github.com> Date: Fri, 9 Sep 2022 17:04:26 +0300 Subject: [PATCH 2/4] Delete PowerOfFour.swift --- Math/PowerOfFour.swift | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 Math/PowerOfFour.swift diff --git a/Math/PowerOfFour.swift b/Math/PowerOfFour.swift deleted file mode 100644 index 50d4ffa..0000000 --- a/Math/PowerOfFour.swift +++ /dev/null @@ -1,29 +0,0 @@ -/** - * 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 - } -} From f108d723182f1d71f22aba4df027c0e861c4f710 Mon Sep 17 00:00:00 2001 From: Sorokin Igor <34890417+srk1nn@users.noreply.github.com> Date: Fri, 9 Sep 2022 17:05:04 +0300 Subject: [PATCH 3/4] [Solution] add solution power of four --- Math/PowerFour.swift | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Math/PowerFour.swift diff --git a/Math/PowerFour.swift b/Math/PowerFour.swift new file mode 100644 index 0000000..94f6c62 --- /dev/null +++ b/Math/PowerFour.swift @@ -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 + } +} From b07e8ed04197c83562eb5113e21f6ff88435375f Mon Sep 17 00:00:00 2001 From: Sorokin Igor <34890417+srk1nn@users.noreply.github.com> Date: Fri, 9 Sep 2022 17:12:40 +0300 Subject: [PATCH 4/4] [Solution] update readme --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 041058e..096da33 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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)| @@ -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