Skip to content

Commit b63f9c5

Browse files
committedApr 27, 2019
[Math] Add a solution to Gary Code
1 parent b053830 commit b63f9c5

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -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

@@ -745,9 +745,9 @@
745745
| [Swift](./Stack/InorderTraversal.swift) | 94 | [Binary Tree Inorder Traversal](https://oj.leetcode.com/problems/binary-tree-inorder-traversal/) | Medium |
746746
| | 93 | [Restore IP Addresses](https://oj.leetcode.com/problems/restore-ip-addresses/) | Medium |
747747
| | 92 | [Reverse Linked List II](https://oj.leetcode.com/problems/reverse-linked-list-ii/) | Medium |
748-
| (./DP/DecodeWays.swift) | 91 | [Decode Ways](https://oj.leetcode.com/problems/decode-ways/) | Medium |
748+
| [Swift](./DP/DecodeWays.swift) | 91 | [Decode Ways](https://oj.leetcode.com/problems/decode-ways/) | Medium |
749749
| [Swift](./DFS/SubsetsII.swift) | 90 | [Subsets II](https://oj.leetcode.com/problems/subsets-ii/) | Medium |
750-
| | 89 | [Gray Code](https://oj.leetcode.com/problems/gray-code/) | Medium |
750+
| [Swift](./Math/GaryCode.swift) | 89 | [Gray Code](https://oj.leetcode.com/problems/gray-code/) | Medium |
751751
| [Swift](./Sort/MergeSortedArray.swift) | 88 | [Merge Sorted Array](https://oj.leetcode.com/problems/merge-sorted-array/) | Easy |
752752
| | 87 | [Scramble String](https://oj.leetcode.com/problems/scramble-string/) | Hard |
753753
| [Swift](./LinkedList/PartitionList.swift) | 86 | [Partition List](https://oj.leetcode.com/problems/partition-list/) | Medium |

0 commit comments

Comments
 (0)
Please sign in to comment.