Skip to content

Commit 2e64e7d

Browse files
committed
Added 89
1 parent 5a25b5b commit 2e64e7d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
| 82 | [Remove Duplicates from Sorted List II](src/82.remove-duplicates-from-sorted-list-ii.py) | Medium | O(N) | O(1) | Linked List | | |
7575
| 84 | [Largest Rectangle in Histogram](src/84.largest-rectangle-in-histogram.py) | Hard | O(N) | O(N) | Array, Stack, Monotonic Stack | | |
7676
| 85 | [Maximal Rectangle](src/85.maximal-rectangle.py) | Hard | O(R\*C\*C) | O(C) | Array, Dynamic Programming, Stack, Matrix, Monotonic Stack | | |
77+
| 89 | [Gray Code](src/89.gray-code.py) | Medium | O(2^N) | O(2^N) | Math, Backtracking, Bit Manipulation | | |
7778
| 91 | [Decode Ways](src/91.decode-ways.py) | Medium | O(N) | O(N) | String, Dynamic Programming | | |
7879
| 92 | [Reverse Linked List II](src/92.reverse-linked-list-ii.py) | Medium | O(N) | O(1) | Linked List | | |
7980
| 94 | [Binary Tree Inorder Traversal](src/94.binary-tree-inorder-traversal.py) | Easy | O(N) | O(N) | Stack, Tree, DFS, Binary Tree | | |

src/89.gray-code.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# @lc app=leetcode id=89 lang=python3
3+
#
4+
# [89] Gray Code
5+
#
6+
7+
8+
# @lc code=start
9+
# TAGS: Math, Backtracking, Bit Manipulation
10+
class Solution:
11+
# Time and Space: O(2^N)
12+
def grayCode(self, n: int) -> List[int]:
13+
ans = [0]
14+
for i in range(n):
15+
highest_bit = 1 << i
16+
for j in reversed(range(highest_bit)):
17+
ans.append(ans[j] + highest_bit)
18+
return ans
19+
20+
21+
# @lc code=end

0 commit comments

Comments
 (0)