Skip to content

Commit dc513d7

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon tag.
1 parent a62c4d2 commit dc513d7

3 files changed

+100
-2
lines changed

Diff for: leetcode/117. Populating Next Right Pointers in Each Node II.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,32 @@ public class Solution {
105105
}
106106
}
107107
}
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+
```

Diff for: leetcode/284. Peeking Iterator.md

+40
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,43 @@ class PeekingIterator implements Iterator<Integer> {
9999
}
100100
}
101101
```
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+
```

Diff for: leetcode/706. Design HashMap.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,34 @@ Note:
127127
* int param_2 = obj.get(key);
128128
* obj.remove(key);
129129
*/
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+
``

0 commit comments

Comments
 (0)