Skip to content

Commit 1905871

Browse files
committed
Today My Solution is Added
1 parent 12d5709 commit 1905871

File tree

28 files changed

+432
-0
lines changed

28 files changed

+432
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Trie {
2+
private class Node {
3+
Node[] childs = new Node[26];
4+
boolean isLeaf;
5+
}
6+
7+
private Node root = new Node();
8+
9+
private int indexForChar(char c) {
10+
return c - 'a';
11+
}
12+
13+
public Trie() {
14+
15+
}
16+
17+
private void insert(String word, Node node) {
18+
if (node == null) return;
19+
if (word.length() == 0) {
20+
node.isLeaf = true;
21+
return;
22+
}
23+
int index = indexForChar(word.charAt(0));
24+
if (node.childs[index] == null) {
25+
node.childs[index] = new Node();
26+
}
27+
insert(word.substring(1), node.childs[index]);
28+
}
29+
30+
public void insert(String word) {
31+
insert(word, root);
32+
}
33+
34+
private boolean search(String word, Node node) {
35+
if (node == null) return false;
36+
if (word.length() == 0) return node.isLeaf;
37+
int index = indexForChar(word.charAt(0));
38+
return search(word.substring(1), node.childs[index]);
39+
}
40+
41+
public boolean search(String word) {
42+
return search(word, root);
43+
}
44+
45+
private boolean startWith(String prefix, Node node) {
46+
if (node == null) return false;
47+
if (prefix.length() == 0) return true;
48+
int index = indexForChar(prefix.charAt(0));
49+
return startWith(prefix.substring(1), node.childs[index]);
50+
}
51+
52+
public boolean startsWith(String prefix) {
53+
return startWith(prefix, root);
54+
}
55+
}
56+
57+
/**
58+
* Your Trie object will be instantiated and called as such:
59+
* Trie obj = new Trie();
60+
* obj.insert(word);
61+
* boolean param_2 = obj.search(word);
62+
* boolean param_3 = obj.startsWith(prefix);
63+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [208. Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)
2+
3+
\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class MinimumSizeSubarraySum {
2+
public int minSubArrayLen(int target, int[] nums) {
3+
int sum = 0, left = 0, res = Integer.MAX_VALUE;
4+
for (int right = 0; right < nums.length; right++) {
5+
sum += nums[right];
6+
while (sum >= target) {
7+
res = Math.min(res, right - left + 1);
8+
sum -= nums[left++];
9+
}
10+
}
11+
return res == Integer.MAX_VALUE ? 0 : res;
12+
}
13+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Stack;
4+
5+
class CourseScheduleIi {
6+
private boolean hasCycle(boolean[] globalMarked, boolean[] localMarked, List<Integer>[] graphic, int curNode, Stack<Integer> postOrder) {
7+
if (localMarked[curNode]) {
8+
return true;
9+
}
10+
if (globalMarked[curNode]) {
11+
return false;
12+
}
13+
globalMarked[curNode] = true;
14+
localMarked[curNode] = true;
15+
for (int nextNode : graphic[curNode]) {
16+
if (hasCycle(globalMarked, localMarked, graphic, nextNode, postOrder)) {
17+
return true;
18+
}
19+
}
20+
localMarked[curNode] = false;
21+
postOrder.push(curNode);
22+
return false;
23+
}
24+
25+
public int[] findOrder(int numCourses, int[][] prerequisites) {
26+
List<Integer>[] graphic = new List[numCourses];
27+
for (int i = 0; i < numCourses; i++) {
28+
graphic[i] = new ArrayList<>();
29+
}
30+
for (int[] pre : prerequisites) {
31+
graphic[pre[0]].add(pre[1]);
32+
}
33+
Stack<Integer> postOder = new Stack<>();
34+
boolean[] globalMarked = new boolean[numCourses];
35+
boolean[] localMarked = new boolean[numCourses];
36+
for (int i = 0; i < numCourses; i++) {
37+
if (hasCycle(globalMarked, localMarked, graphic, i, postOder)) {
38+
return new int[0];
39+
}
40+
}
41+
int[] orders = new int[numCourses];
42+
for (int i = numCourses - 1; i >= 0; i--) {
43+
orders[i] = postOder.pop();
44+
}
45+
return orders;
46+
}
47+
}

0210.course-schedule-ii/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [210. Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.Arrays;
2+
3+
class KthLargestElementInAnArray {
4+
public int findKthLargest(int[] nums, int k) {
5+
Arrays.sort(nums);
6+
return nums[nums.length - k];
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class ContainsDuplicate {
5+
public boolean containsDuplicate(int[] nums) {
6+
Set<Integer> set = new HashSet<>();
7+
for (int num : nums) {
8+
set.add(num);
9+
}
10+
return set.size() < nums.length;
11+
}
12+
}

0217.contains-duplicate/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
2+

0218.the-skyline-problem/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [218. The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
class TheSkylineProblem {
4+
public List<List<Integer>> getSkyline(int[][] buildings) {
5+
List<List<Integer>> res = new ArrayList<>();
6+
List<int[]> height = new ArrayList<>();
7+
for (int[] b : buildings) {
8+
height.add(new int[]{b[0], -b[2]});
9+
height.add(new int[]{b[1], b[2]});
10+
}
11+
height.sort((a, b) -> {
12+
if (a[0] != b[0]) return a[0] - b[0];
13+
else return a[1] - b[1];
14+
});
15+
Queue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));
16+
pq.offer(0);
17+
int prev = 0;
18+
for (int[] h : height) {
19+
if (h[1] < 0) pq.offer(-h[1]);
20+
else pq.remove(h[1]);
21+
int cur = pq.peek() != null ? pq.peek() : 0;
22+
if (prev != cur) {
23+
res.add(Arrays.asList(h[0], cur));
24+
prev = cur;
25+
}
26+
}
27+
return res;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class ContainsDuplicateIi {
5+
public boolean containsNearbyDuplicate(int[] nums, int k) {
6+
Map<Integer, Integer> map = new HashMap<>();
7+
for (int i = 0; i < nums.length; i++) {
8+
if (map.containsKey(nums[i])) {
9+
if (Math.abs(map.get(nums[i]) - i) <= k) return true;
10+
}
11+
map.put(nums[i], i);
12+
}
13+
return false;
14+
}
15+
}

0219.contains-duplicate-ii/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class CountCompleteTreeNodes {
17+
private int leftHeight(TreeNode node) {
18+
int h = 0;
19+
while (node != null) {
20+
h++;
21+
node = node.left;
22+
}
23+
return h;
24+
}
25+
26+
public int countNodes(TreeNode root) {
27+
if (root == null) return 0;
28+
29+
int l = leftHeight(root.left);
30+
int r = leftHeight(root.right);
31+
if (l == r)
32+
return countNodes(root.right) + (1 << l);
33+
return countNodes(root.left) + (1 << r);
34+
}
35+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [222. Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)

0223.rectangle-area/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [223. Rectangle Area](https://leetcode.com/problems/rectangle-area/)
2+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class RectangleArea {
2+
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
3+
int areaOfA = (ax2 - ax1) * (ay2 - ay1);
4+
int areaOfB = (bx2 - bx1) * (by2 - by1);
5+
6+
int left = Math.max(ax1, bx1);
7+
int right = Math.min(ax2, bx2);
8+
int xOverlap = right - left;
9+
10+
int top = Math.min(ay2, by2);
11+
int bottom = Math.max(ay1, by1);
12+
int yOverlap = top - bottom;
13+
14+
int areaOfOverlap = 0;
15+
if (xOverlap > 0 && yOverlap > 0) {
16+
areaOfOverlap = xOverlap * yOverlap;
17+
}
18+
19+
return areaOfA + areaOfB - areaOfOverlap;
20+
}
21+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Stack;
2+
3+
class BasicCalculator {
4+
public int calculate(String s) {
5+
Stack<Integer> stack = new Stack<>();
6+
int result = 0, number = 0, sign = 1;
7+
for (int i = 0; i < s.length(); i++) {
8+
char c = s.charAt(i);
9+
if (Character.isDigit(c)) {
10+
number = 10 * number + (int)(c - '0');
11+
} else if (c == '+') {
12+
result += sign * number;
13+
number = 0;
14+
sign = 1;
15+
} else if (c == '-') {
16+
result += sign * number;
17+
number = 0;
18+
sign = -1;
19+
} else if (c == '(') {
20+
stack.push(result);
21+
stack.push(sign);
22+
sign = 1;
23+
result = 0;
24+
} else if (c == ')') {
25+
result += sign * number;
26+
number = 0;
27+
result *= stack.pop();
28+
result += stack.pop();
29+
}
30+
}
31+
if (number != 0) result += sign * number;
32+
return result;
33+
}
34+
}

0224.basic-calculator/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [224. Basic Calculator](https://leetcode.com/problems/basic-calculator/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
class MyStack {
5+
private Queue<Integer> queue;
6+
7+
public MyStack() {
8+
queue = new LinkedList<>();
9+
}
10+
11+
public void push(int x) {
12+
queue.add(x);
13+
int cnt = queue.size();
14+
while (cnt -- > 1) {
15+
queue.add(queue.poll());
16+
}
17+
}
18+
19+
public int pop() {
20+
return queue.remove();
21+
}
22+
23+
public int top() {
24+
if (queue.isEmpty()) return 0;
25+
return queue.peek();
26+
}
27+
28+
public boolean empty() {
29+
return queue.isEmpty();
30+
}
31+
}
32+
33+
/**
34+
* Your MyStack object will be instantiated and called as such:
35+
* MyStack obj = new MyStack();
36+
* obj.push(x);
37+
* int param_2 = obj.pop();
38+
* int param_3 = obj.top();
39+
* boolean param_4 = obj.empty();
40+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [225. Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class InvertBinaryTree {
17+
public TreeNode invertTree(TreeNode root) {
18+
if (root == null) return null;
19+
TreeNode left = root.left;
20+
root.left = invertTree(root.right);
21+
root.right = invertTree(left);
22+
return root;
23+
}
24+
}

0 commit comments

Comments
 (0)