Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1ff8252

Browse files
solves gray code
1 parent 89f8d9d commit 1ff8252

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
8080
| 86 | [Partition List](https://leetcode.com/problems/partition-list) | [![Java](assets/java.png)](src/PartitionList.java) | |
8181
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
82+
| 89 | [Gray Code](https://leetcode.com/problems/gray-code) | [![Java](assets/java.png)](src/GrayCode.java) | |
8283
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |
8384
| 100 | [Same Tree](https://leetcode.com/problems/same-tree) | [![Java](assets/java.png)](src/SameTree.java) [![Python](assets/python.png)](python/same_tree.py) | |
8485
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree) | [![Java](assets/java.png)](src/SymmetricTree.java) [![Python](assets/python.png)](python/symmetric_tree.py) | |

src/GrayCode.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/gray-code
2+
// T: O(2 ^ n)
3+
// S: O(2 ^ n)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class GrayCode {
9+
public List<Integer> grayCode(int n) {
10+
final List<Integer> result = new ArrayList<>(1 << n);
11+
result.add(0);
12+
for (int i = 0, factor = 1 ; i < n ; i++, factor *= 2) {
13+
for (int j = factor - 1 ; j >= 0 ; j--) {
14+
result.add(result.get(j) + factor);
15+
}
16+
}
17+
return result;
18+
}
19+
}

0 commit comments

Comments
 (0)