Skip to content

Commit 95bb6cc

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent fdb0087 commit 95bb6cc

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

leetcode/210. Course Schedule II.md

+42
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,46 @@ class Solution {
195195
return remain.isEmpty() ? order: new int[0];
196196
}
197197
}
198+
```
199+
200+
### Amazon Session
201+
* Method 1: Topological sort
202+
```Java
203+
class Solution {
204+
public int[] findOrder(int numCourses, int[][] prerequisites) {
205+
Map<Integer, List<Integer>> map = new HashMap<>();
206+
int[] indegree = new int[numCourses];
207+
for(int[] req : prerequisites){
208+
indegree[req[0]]++;
209+
List<Integer> list = map.getOrDefault(req[1], new ArrayList<>());
210+
list.add(req[0]);
211+
map.put(req[1], list);
212+
}
213+
Queue<Integer> q = new LinkedList<>();
214+
for(int i = 0; i < numCourses; i++){
215+
if(indegree[i] == 0){
216+
q.offer(i);
217+
}
218+
}
219+
List<Integer> result = new ArrayList<>();
220+
while(!q.isEmpty()){
221+
int cur = q.poll();
222+
result.add(cur);
223+
if(!map.containsKey(cur)) continue;
224+
List<Integer> neighbours = map.get(cur);
225+
for(Integer n : neighbours){
226+
if(--indegree[n] == 0){
227+
q.offer(n);
228+
}
229+
}
230+
}
231+
if(result.size() != numCourses) return new int[0];
232+
int[] res = new int[numCourses];
233+
int index = 0;
234+
while(index < numCourses){
235+
res[index] = result.get(index++);
236+
}
237+
return res;
238+
}
239+
}
198240
```

leetcode/772. Basic Calculator III.md

+47
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,50 @@ Some examples:
6767
}
6868
}
6969
```
70+
71+
### Amazon session
72+
* Method 1: recursion + stack
73+
```Java
74+
class Solution {
75+
private char[] arr;
76+
private int i;
77+
public int calculate(String s) {
78+
this.arr = s.toCharArray();
79+
return parse();
80+
}
81+
private int parse(){
82+
char oper = '+';
83+
int sum = 0;
84+
Stack<Integer> stack = new Stack<>();
85+
while(i < arr.length){
86+
char c = arr[i];
87+
if(c == '('){
88+
i++;
89+
addToStack(stack, oper, parse());
90+
}else if(Character.isDigit(c)){
91+
int cur = c - '0';
92+
i++;
93+
while(i < arr.length && Character.isDigit(arr[i])){
94+
cur = cur * 10 + arr[i++] - '0';
95+
}
96+
addToStack(stack, oper, cur);
97+
}else if(c == ')'){
98+
while(!stack.isEmpty()) sum += stack.pop();
99+
i++;
100+
return sum;
101+
}else if(c == '+' || c == '-' || c == '*' || c == '/'){
102+
oper = c;
103+
i++;
104+
}else if(c == ' ') i++;
105+
}
106+
while(!stack.isEmpty()) sum += stack.pop();
107+
return sum;
108+
}
109+
private void addToStack(Stack<Integer> stack, char c, int num){
110+
if(c == '+') stack.push(num);
111+
else if(c == '-') stack.push(-num);
112+
else if(c == '*') stack.push(stack.pop() * num);
113+
else stack.push(stack.pop() / num);
114+
}
115+
}
116+
```

leetcode/993. Cousins in Binary Tree.md

+42
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,45 @@ Note:
7575
}
7676
}
7777
```
78+
79+
### Amazon Session
80+
* Method 1: bfs
81+
```Java
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode(int x) { val = x; }
89+
* }
90+
*/
91+
class Solution {
92+
public boolean isCousins(TreeNode root, int x, int y) {
93+
Queue<TreeNode> q = new LinkedList<>();
94+
if(root.val == x || root.val == y) return false;
95+
q.offer(root);
96+
while(!q.isEmpty()){
97+
Set<Integer> value = new HashSet<>();
98+
int size = q.size();
99+
for(int i = 0; i < size; i++){
100+
TreeNode node = q.poll();
101+
if(node.left != null && node.right != null){
102+
if((node.left.val == x && node.right.val == y)
103+
|| (node.left.val == y && node.right.val == x)) return false;
104+
}
105+
if(node.left != null){
106+
q.offer(node.left);
107+
value.add(node.left.val);
108+
}
109+
if(node.right != null){
110+
q.offer(node.right);
111+
value.add(node.right.val);
112+
}
113+
}
114+
if(value.contains(x) && value.contains(y)) return true;
115+
}
116+
return false;
117+
}
118+
}
119+
```

0 commit comments

Comments
 (0)