Skip to content

[Math] Add a solution to Gary Code #239

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

Merged
merged 2 commits into from
May 4, 2019
Merged
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
18 changes: 18 additions & 0 deletions Math/GaryCode.swift
Original file line number Diff line number Diff line change
@@ -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..<n {
codes += codes.reversed().map { $0 | 1 << i }
}
return codes
}
}
9 changes: 5 additions & 4 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 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.
[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.

## Contributors

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

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