Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdc513d7

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon tag.
1 parenta62c4d2 commitdc513d7

3 files changed

+100
-2
lines changed

‎leetcode/117. Populating Next Right Pointers in Each Node II.md

Lines changed: 29 additions & 1 deletion
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+
```

‎leetcode/284. Peeking Iterator.md

Lines changed: 40 additions & 0 deletions
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+
```

‎leetcode/706. Design HashMap.md

Lines changed: 31 additions & 1 deletion
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+
###AmazonSession
133+
*Method1:
134+
```Java
135+
classMyHashMap {
136+
privateint[] values;
137+
privatestaticfinalintLEN=1000001;
138+
/** Initialize your data structure here.*/
139+
publicMyHashMap() {
140+
this.values=newint[LEN];
141+
Arrays.fill(this.values,-1);
142+
}
143+
144+
/** value will always be non-negative.*/
145+
publicvoidput(intkey,intvalue) {
146+
int index=newInteger(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+
publicintget(intkey) {
152+
returnthis.values[newInteger(key).hashCode()%LEN];
153+
}
154+
155+
/** Removes the mapping of the specified value key if this map contains a mapping for the key*/
156+
publicvoidremove(intkey) {
157+
this.values[newInteger(key).hashCode()%LEN]=-1;
158+
}
159+
}
160+
``

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp