File tree 2 files changed +20
-0
lines changed
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 79
79
| 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 ) | |
80
80
| 86 | [ Partition List] ( https://leetcode.com/problems/partition-list ) | [ ![ Java] ( assets/java.png )] ( src/PartitionList.java ) | |
81
81
| 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 ) | |
82
83
| 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 ) | |
83
84
| 100 | [ Same Tree] ( https://leetcode.com/problems/same-tree ) | [ ![ Java] ( assets/java.png )] ( src/SameTree.java ) [ ![ Python] ( assets/python.png )] ( python/same_tree.py ) | |
84
85
| 101 | [ Symmetric Tree] ( https://leetcode.com/problems/symmetric-tree ) | [ ![ Java] ( assets/java.png )] ( src/SymmetricTree.java ) [ ![ Python] ( assets/python.png )] ( python/symmetric_tree.py ) | |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments