Skip to content

Commit 618427f

Browse files
author
Botao Xiao
committed
[Function add]:1. Add leetcode solutions with tag amazon.
1 parent 13af7bf commit 618427f

6 files changed

+471
-2
lines changed

leetcode/126. Word Ladder II.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,66 @@ Explanation: The endWord "cog" is not in wordList, therefore no possible transfo
138138
}
139139
}
140140
}
141-
```
141+
```
142+
143+
### Second Time
144+
* Method 1: bfs + dfs
145+
```Java
146+
class Solution {
147+
private List<List<String>> result;
148+
private Map<String, List<String>> map;
149+
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
150+
Set<String> wordSet = new HashSet<>(wordList);
151+
wordSet.remove(beginWord);
152+
this.result = new ArrayList<>();
153+
if(!wordSet.contains(endWord)) return this.result;
154+
this.map = new HashMap<>();
155+
Queue<String> q = new LinkedList<>();
156+
q.offer(beginWord);
157+
boolean found = false;
158+
int len = beginWord.length();
159+
while(!found && !q.isEmpty()){
160+
int size = q.size();
161+
Set<String> nextLevel = new HashSet<>();
162+
for(int i = 0; i < size; i++){
163+
String temp = q.poll();
164+
if(temp.equals(endWord)) found = true;
165+
char[] arr = temp.toCharArray();
166+
for(int j = 0; j < len; j++){
167+
char origin = arr[j];
168+
for(char c = 'a'; c <= 'z'; c++){
169+
if(c == origin) continue;
170+
arr[j] = c;
171+
String next = new String(arr);
172+
if(wordSet.contains(next) || nextLevel.contains(next)){
173+
List<String> list = map.containsKey(temp) ? map.get(temp): new ArrayList<>();
174+
list.add(next);
175+
map.put(temp, list);
176+
nextLevel.add(next);
177+
if(wordSet.contains(next)) q.offer(next);
178+
wordSet.remove(next);
179+
}
180+
}
181+
arr[j] = origin;
182+
}
183+
}
184+
}
185+
List<String> temp = new ArrayList<>();
186+
temp.add(beginWord);
187+
dfs(temp, endWord, beginWord);
188+
return this.result;
189+
}
190+
private void dfs(List<String> temp, String end, String cur){
191+
if(cur.equals(end)) this.result.add(new ArrayList<String>(temp));
192+
else{
193+
if(!map.containsKey(cur)) return;
194+
List<String> adj = map.get(cur);
195+
for(String s: adj){
196+
temp.add(s);
197+
dfs(temp, end, s);
198+
temp.remove(temp.size() - 1);
199+
}
200+
}
201+
}
202+
}
203+
```

leetcode/173. Binary Search Tree Iterator.md

+95
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,98 @@ public class BSTIterator {
143143
* while (i.hasNext()) v[f()] = i.next();
144144
*/
145145
```
146+
147+
### Third Time
148+
* Method 1: Recursion
149+
```Java
150+
/**
151+
* Definition for a binary tree node.
152+
* public class TreeNode {
153+
* int val;
154+
* TreeNode left;
155+
* TreeNode right;
156+
* TreeNode(int x) { val = x; }
157+
* }
158+
*/
159+
class BSTIterator {
160+
private Queue<Integer> q;
161+
public BSTIterator(TreeNode root) {
162+
q = new LinkedList<>();
163+
inorder(root);
164+
}
165+
private void inorder(TreeNode node){
166+
if(node == null) return;
167+
inorder(node.left);
168+
q.offer(node.val);
169+
inorder(node.right);
170+
}
171+
172+
/** @return the next smallest number */
173+
public int next() {
174+
return q.poll();
175+
}
176+
177+
/** @return whether we have a next smallest number */
178+
public boolean hasNext() {
179+
return !q.isEmpty();
180+
}
181+
}
182+
183+
/**
184+
* Your BSTIterator object will be instantiated and called as such:
185+
* BSTIterator obj = new BSTIterator(root);
186+
* int param_1 = obj.next();
187+
* boolean param_2 = obj.hasNext();
188+
*/
189+
```
190+
191+
* Method 2: loop
192+
```Java
193+
/**
194+
* Definition for a binary tree node.
195+
* public class TreeNode {
196+
* int val;
197+
* TreeNode left;
198+
* TreeNode right;
199+
* TreeNode(int x) { val = x; }
200+
* }
201+
*/
202+
class BSTIterator {
203+
Stack<TreeNode> stack;
204+
public BSTIterator(TreeNode root) {
205+
this.stack = new Stack<>();
206+
if(root == null) return;
207+
stack.push(root);
208+
while(root.left != null){
209+
stack.push(root.left);
210+
root = root.left;
211+
}
212+
}
213+
214+
/** @return the next smallest number */
215+
public int next() {
216+
TreeNode node = stack.pop();
217+
if(node.right != null){
218+
TreeNode temp = node.right;
219+
stack.push(temp);
220+
while(temp.left != null){
221+
stack.push(temp.left);
222+
temp = temp.left;
223+
}
224+
}
225+
return node.val;
226+
}
227+
228+
/** @return whether we have a next smallest number */
229+
public boolean hasNext() {
230+
return !stack.isEmpty();
231+
}
232+
}
233+
234+
/**
235+
* Your BSTIterator object will be instantiated and called as such:
236+
* BSTIterator obj = new BSTIterator(root);
237+
* int param_1 = obj.next();
238+
* boolean param_2 = obj.hasNext();
239+
*/
240+
```

leetcode/252. Meeting Rooms.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
## 252. Meeting Rooms
2+
3+
### Question
4+
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.
5+
6+
```
7+
Example 1:
8+
9+
Input: [[0,30],[5,10],[15,20]]
10+
Output: false
11+
Example 2:
12+
13+
Input: [[7,10],[2,4]]
14+
Output: true
15+
```
16+
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
17+
218
### Thinking:
319
* Method 1:
420

@@ -17,4 +33,27 @@ public class MeetingRooms {
1733
System.out.println(canAttend(t));
1834
}
1935
}
20-
```
36+
```
37+
38+
### Second Time
39+
* Method 1: Sort
40+
```Java
41+
class Solution {
42+
public boolean canAttendMeetings(int[][] intervals) {
43+
int len = intervals.length;
44+
int[] startTime = new int[len];
45+
int[] endTime = new int[len];
46+
int count = 0;
47+
for(int[] interval: intervals){
48+
startTime[count] = interval[0];
49+
endTime[count++] = interval[1];
50+
}
51+
Arrays.sort(startTime);
52+
Arrays.sort(endTime);
53+
for(int i = 1; i < len; i++){
54+
if(startTime[i] < endTime[i - 1]) return false;
55+
}
56+
return true;
57+
}
58+
}
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 346. Moving Average from Data Stream
2+
3+
### Question
4+
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
5+
6+
```
7+
Example:
8+
9+
MovingAverage m = new MovingAverage(3);
10+
m.next(1) = 1
11+
m.next(10) = (1 + 10) / 2
12+
m.next(3) = (1 + 10 + 3) / 3
13+
m.next(5) = (10 + 3 + 5) / 3
14+
```
15+
16+
### Solution
17+
* Method 1: Array
18+
```Java
19+
class MovingAverage {
20+
private int[] arr;
21+
private int size;
22+
private int index;
23+
/** Initialize your data structure here. */
24+
public MovingAverage(int size) {
25+
this.size = size;
26+
this.arr = new int[size];
27+
}
28+
public double next(int val) {
29+
arr[(index++) % this.size] = val;
30+
double sum = 0D;
31+
for(int v : arr)
32+
sum += v;
33+
return sum / (index <= size ? index: this.size);
34+
}
35+
}
36+
/**
37+
* Your MovingAverage object will be instantiated and called as such:
38+
* MovingAverage obj = new MovingAverage(size);
39+
* double param_1 = obj.next(val);
40+
*/
41+
```
42+
43+
* Method 2: Queue
44+
```Java
45+
class MovingAverage {
46+
private Queue<Integer> q;
47+
private int size;
48+
private double sum;
49+
/** Initialize your data structure here. */
50+
public MovingAverage(int size) {
51+
this.size = size;
52+
q = new LinkedList<>();
53+
}
54+
55+
public double next(int val) {
56+
q.offer(val);
57+
sum += val;
58+
while(!q.isEmpty() && q.size() > size){
59+
sum -= q.poll();
60+
}
61+
return sum / q.size();
62+
}
63+
}
64+
65+
/**
66+
* Your MovingAverage object will be instantiated and called as such:
67+
* MovingAverage obj = new MovingAverage(size);
68+
* double param_1 = obj.next(val);
69+
*/
70+
```

0 commit comments

Comments
 (0)