File tree 3 files changed +100
-2
lines changed
3 files changed +100
-2
lines changed Original file line number Diff line number Diff line change @@ -105,4 +105,32 @@ public class Solution {
105
105
}
106
106
}
107
107
}
108
- ```
108
+ ```
109
+
110
+ ### Amazon session
111
+ * Method 1: BFS using recursion Space complexity O(1).
112
+ ```Java
113
+ class Solution {
114
+ public Node connect(Node root) {
115
+ recursion(root);
116
+ return root;
117
+ }
118
+ private void recursion(Node node){
119
+ if(node == null) return;
120
+ Node dummy = new Node();
121
+ Node cur = dummy;
122
+ while(node != null){ // for current level.
123
+ if(node.left != null){ // append all child nodes.
124
+ cur.next = node.left;
125
+ cur = cur.next;
126
+ }
127
+ if(node.right != null){
128
+ cur.next = node.right;
129
+ cur = cur.next;
130
+ }
131
+ node = node.next;
132
+ }
133
+ recursion(dummy.next); // recursion to next level.
134
+ }
135
+ }
136
+ ```
Original file line number Diff line number Diff line change @@ -99,3 +99,43 @@ class PeekingIterator implements Iterator<Integer> {
99
99
}
100
100
}
101
101
```
102
+
103
+ ### Amazon session
104
+ * Method 1: use a peek holder instead of a list.
105
+ ```Java
106
+ // Java Iterator interface reference:
107
+ // https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
108
+ class PeekingIterator implements Iterator<Integer> {
109
+ private Integer peek;
110
+ private Iterator<Integer> it;
111
+ public PeekingIterator(Iterator<Integer> iterator) {
112
+ // initialize any member here.
113
+ this.it = iterator;
114
+ }
115
+
116
+ // Returns the next element in the iteration without advancing the iterator.
117
+ public Integer peek() {
118
+ if(peek == null) {
119
+ peek = it.next();
120
+ }
121
+ return peek;
122
+ }
123
+
124
+ // hasNext() and next() should behave the same as in the Iterator interface.
125
+ // Override them if needed.
126
+ @Override
127
+ public Integer next() {
128
+ if(peek == null) return it.next();
129
+ else{
130
+ Integer res = new Integer(peek);
131
+ peek = null;
132
+ return res;
133
+ }
134
+ }
135
+
136
+ @Override
137
+ public boolean hasNext() {
138
+ return peek != null || it.hasNext();
139
+ }
140
+ }
141
+ ```
Original file line number Diff line number Diff line change @@ -127,4 +127,34 @@ Note:
127
127
* int param_2 = obj.get(key);
128
128
* obj.remove(key);
129
129
*/
130
- ```
130
+ ```
131
+
132
+ ### Amazon Session
133
+ * Method 1 :
134
+ ```Java
135
+ class MyHashMap {
136
+ private int [] values;
137
+ private static final int LEN = 1000001 ;
138
+ /* * Initialize your data structure here. */
139
+ public MyHashMap () {
140
+ this . values = new int [LEN ];
141
+ Arrays . fill(this . values, - 1 );
142
+ }
143
+
144
+ /* * value will always be non-negative. */
145
+ public void put (int key , int value ) {
146
+ int index = new Integer (key). hashCode() % LEN ;
147
+ this . values[index] = value;
148
+ }
149
+
150
+ /* * Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
151
+ public int get (int key ) {
152
+ return this . values[new Integer (key). hashCode() % LEN ];
153
+ }
154
+
155
+ /* * Removes the mapping of the specified value key if this map contains a mapping for the key */
156
+ public void remove (int key ) {
157
+ this . values[new Integer (key). hashCode() % LEN ] = - 1 ;
158
+ }
159
+ }
160
+ ``
You can’t perform that action at this time.
0 commit comments