From b63f9c514891f94298390716ecd5c853b5082ddd Mon Sep 17 00:00:00 2001 From: Qingfan Wu Date: Sat, 27 Apr 2019 15:52:26 -0700 Subject: [PATCH 1/2] [Math] Add a solution to Gary Code --- Math/GaryCode.swift | 18 ++++++++++++++++++ README.md | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 Math/GaryCode.swift diff --git a/Math/GaryCode.swift b/Math/GaryCode.swift new file mode 100644 index 00000000..0397d0f4 --- /dev/null +++ b/Math/GaryCode.swift @@ -0,0 +1,18 @@ +/** + * Question Link: https://leetcode.com/problems/gray-code/ + * Primary idea: the result of n can be derived from (n - 1) by reversing + the order and prepending 1 to each number's binary representation + * + * Time Complexity: O(n), Space Complexity: O(2^n) + * + */ + +class GaryCode { + func grayCode(_ n: Int) -> [Int] { + var codes = [0] + for i in 0.. Date: Sat, 4 May 2019 16:55:12 -0700 Subject: [PATCH 2/2] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 057d6445..c598607c 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 800+ questions. Currently we have 272 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 800+ questions. Currently we have 273 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. ## Contributors @@ -304,6 +304,7 @@ [Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [Swift](./Math/ContainerMostWater.swift)| Medium| O(n)| O(1)| [Counting Bits](https://leetcode.com/problems/counting-bits/)| [Swift](./Math/CountingBits.swift)| Medium| O(n)| O(n)| [K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/)| [Swift](./Math/KthSmallestLexicographicalOrder.swift)| Hard| O(n)| O(1)| +[Gary Code](https://leetcode.com/problems/gray-code/)| [Swift](./Math/GaryCode.swift)| Medium| O(n)| O(2^n)| ## Search | Title | Solution | Difficulty | Time | Space |