Skip to content

Commit 587e642

Browse files
authored
Merge pull request #239 from wqfan/master
[Math] Add a solution to Gary Code
2 parents aae4031 + a49ea38 commit 587e642

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Math/GaryCode.swift

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/gray-code/
3+
* Primary idea: the result of n can be derived from (n - 1) by reversing
4+
the order and prepending 1 to each number's binary representation
5+
*
6+
* Time Complexity: O(n), Space Complexity: O(2^n)
7+
*
8+
*/
9+
10+
class GaryCode {
11+
func grayCode(_ n: Int) -> [Int] {
12+
var codes = [0]
13+
for i in 0..<n {
14+
codes += codes.reversed().map { $0 | 1 << i }
15+
}
16+
return codes
17+
}
18+
}

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 272 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 273 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -83,7 +83,7 @@
8383
[Gas Station](https://leetcode.com/problems/gas-station/)| [Swift](./Array/GasStation.swift)| Medium| O(n)| O(1)|
8484
[Game of Life](https://leetcode.com/problems/game-of-life/)| [Swift](./Array/GameLife.swift)| Medium| O(n)| O(1)|
8585
[Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Swift](./Array/TaskScheduler.swift)| Medium| O(nlogn)| O(n)|
86-
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
86+
[Sliding Window Maximum ](https://leetcode.com/problems/sliding-window-maximum/)| [Swift](./Array/SlidingWindowMaximum.swift)| Hard| O(n)| O(n)|
8787
[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [Swift](./Array/LongestConsecutiveSequence.swift)| Hard| O(n)| O(n)|
8888
[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/)| [Swift](./Array/CreateMaximumNumber.swift)| Hard| O(n^2)| O(n)|
8989

@@ -306,6 +306,7 @@
306306
[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [Swift](./Math/ContainerMostWater.swift)| Medium| O(n)| O(1)|
307307
[Counting Bits](https://leetcode.com/problems/counting-bits/)| [Swift](./Math/CountingBits.swift)| Medium| O(n)| O(n)|
308308
[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)|
309+
[Gary Code](https://leetcode.com/problems/gray-code/)| [Swift](./Math/GaryCode.swift)| Medium| O(n)| O(2^n)|
309310

310311
## Search
311312
| Title | Solution | Difficulty | Time | Space |
@@ -748,9 +749,9 @@
748749
| [Swift](./Stack/InorderTraversal.swift) | 94 | [Binary Tree Inorder Traversal](https://oj.leetcode.com/problems/binary-tree-inorder-traversal/) | Medium |
749750
| | 93 | [Restore IP Addresses](https://oj.leetcode.com/problems/restore-ip-addresses/) | Medium |
750751
| | 92 | [Reverse Linked List II](https://oj.leetcode.com/problems/reverse-linked-list-ii/) | Medium |
751-
| (./DP/DecodeWays.swift) | 91 | [Decode Ways](https://oj.leetcode.com/problems/decode-ways/) | Medium |
752+
| [Swift](./DP/DecodeWays.swift) | 91 | [Decode Ways](https://oj.leetcode.com/problems/decode-ways/) | Medium |
752753
| [Swift](./DFS/SubsetsII.swift) | 90 | [Subsets II](https://oj.leetcode.com/problems/subsets-ii/) | Medium |
753-
| | 89 | [Gray Code](https://oj.leetcode.com/problems/gray-code/) | Medium |
754+
| [Swift](./Math/GaryCode.swift) | 89 | [Gray Code](https://oj.leetcode.com/problems/gray-code/) | Medium |
754755
| [Swift](./Sort/MergeSortedArray.swift) | 88 | [Merge Sorted Array](https://oj.leetcode.com/problems/merge-sorted-array/) | Easy |
755756
| | 87 | [Scramble String](https://oj.leetcode.com/problems/scramble-string/) | Hard |
756757
| [Swift](./LinkedList/PartitionList.swift) | 86 | [Partition List](https://oj.leetcode.com/problems/partition-list/) | Medium |

0 commit comments

Comments
 (0)