Skip to content

Commit 9761fa0

Browse files
committed
Added 3 solutions & modified 1 solution
1 parent bddfe38 commit 9761fa0

4 files changed

+158
-31
lines changed

Medium/Construct Quad Tree.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
// Definition for a QuadTree node.
3+
class Node {
4+
public boolean val;
5+
public boolean isLeaf;
6+
public Node topLeft;
7+
public Node topRight;
8+
public Node bottomLeft;
9+
public Node bottomRight;
10+
11+
public Node() {}
12+
13+
public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
14+
val = _val;
15+
isLeaf = _isLeaf;
16+
topLeft = _topLeft;
17+
topRight = _topRight;
18+
bottomLeft = _bottomLeft;
19+
bottomRight = _bottomRight;
20+
}
21+
};
22+
*/
23+
class Solution {
24+
public Node construct(int[][] grid) {
25+
return helper(grid, 0, 0, grid.length);
26+
}
27+
28+
private Node helper(int[][] grid, int x, int y, int len) {
29+
if (len == 1) {
30+
return new Node(grid[x][y] != 0, true, null, null, null, null);
31+
}
32+
33+
Node result = new Node();
34+
Node topLeft = helper(grid, x, y, len / 2);
35+
Node topRight = helper(grid, x, y + len / 2, len / 2);
36+
Node bottomLeft = helper(grid, x + len / 2, y, len / 2);
37+
Node bottomRight = helper(grid, x + len / 2, y + len / 2, len / 2);
38+
39+
if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf
40+
&& topLeft.val == topRight.val && topRight.val == bottomLeft.val && bottomLeft.val == bottomRight.val) {
41+
result.isLeaf = true;
42+
result.val = topLeft.val;
43+
} else {
44+
result.topLeft = topLeft;
45+
result.topRight = topRight;
46+
result.bottomLeft = bottomLeft;
47+
result.bottomRight = bottomRight;
48+
}
49+
50+
return result;
51+
}
52+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public String fractionToDecimal(int numerator, int denominator) {
3+
if (numerator == 0) {
4+
return "0";
5+
}
6+
7+
StringBuilder fraction = new StringBuilder();
8+
if (numerator < 0 ^ denominator < 0) {
9+
fraction.append("-");
10+
}
11+
12+
long dividend = Math.abs(Long.valueOf(numerator));
13+
long divisor = Math.abs(Long.valueOf(denominator));
14+
15+
fraction.append(String.valueOf(dividend / divisor));
16+
long remainder = dividend % divisor;
17+
18+
if (remainder == 0) {
19+
return fraction.toString();
20+
}
21+
22+
fraction.append('.');
23+
Map<Long, Integer> map = new HashMap<>();
24+
while (remainder != 0) {
25+
if (map.containsKey(remainder)) {
26+
fraction.insert(map.get(remainder), "(");
27+
fraction.append(')');
28+
break;
29+
}
30+
31+
map.put(remainder, fraction.length());
32+
remainder *= 10;
33+
fraction.append(String.valueOf(remainder / divisor));
34+
remainder %= divisor;
35+
}
36+
37+
return fraction.toString();
38+
}
39+
}

Medium/Interval List Intersections.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
1-
/**
2-
* Definition for an interval.
3-
* public class Interval {
4-
* int start;
5-
* int end;
6-
* Interval() { start = 0; end = 0; }
7-
* Interval(int s, int e) { start = s; end = e; }
8-
* }
9-
*/
101
class Solution {
11-
public Interval[] intervalIntersection(Interval[] A, Interval[] B) {
12-
int startA = 0;
13-
int startB = 0;
14-
int endA = A.length;
15-
int endB = B.length;
16-
17-
List<Interval> intervals = new ArrayList<>();
18-
19-
while (startA < endA && startB < endB) {
20-
if (A[startA].end < B[startB].start) {
21-
startA++;
2+
public int[][] intervalIntersection(int[][] A, int[][] B) {
3+
List<int[]> intersections = new ArrayList<>();
4+
int idx1 = 0;
5+
int idx2 = 0;
6+
7+
while (idx1 < A.length && idx2 < B.length) {
8+
if (A[idx1][0] > B[idx2][1]) {
9+
idx2++;
2210
continue;
2311
}
2412

25-
if (A[startA].start > B[startB].end) {
26-
startB++;
13+
if (A[idx1][1] < B[idx2][0]) {
14+
idx1++;
2715
continue;
2816
}
2917

30-
int intervalStart = Math.max(A[startA].start, B[startB].start);
31-
int intervalEnd = Math.min(A[startA].end, B[startB].end);
18+
int maxStart = Math.max(A[idx1][0], B[idx2][0]);
19+
int minEnd = Math.min(A[idx1][1], B[idx2][1]);
3220

33-
intervals.add(new Interval(intervalStart, intervalEnd));
21+
int[] interval = {maxStart, minEnd};
22+
intersections.add(interval);
3423

35-
if (A[startA].end < B[startB].end) {
36-
startA++;
24+
if (A[idx1][1] > B[idx2][1]) {
25+
idx2++;
3726
}
3827
else {
39-
startB++;
28+
idx1++;
4029
}
4130
}
4231

43-
Interval[] ans = new Interval[intervals.size()];
44-
for (int i=0; i<ans.length; i++) {
45-
ans[i] = intervals.get(i);
32+
int[][] ans = new int[intersections.size()][2];
33+
for (int i = 0; i < intersections.size(); i++) {
34+
ans[i] = intersections.get(i);
4635
}
4736

4837
return ans;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public int[] nextLargerNodes(ListNode head) {
11+
ListNode rev = reverse(head);
12+
Stack<Integer> stack = new Stack<>();
13+
List<Integer> list = new ArrayList<>();
14+
15+
while (rev != null) {
16+
while (!stack.isEmpty() && stack.peek() <= rev.val) {
17+
stack.pop();
18+
}
19+
20+
list.add(stack.isEmpty() ? 0 : stack.peek());
21+
stack.push(rev.val);
22+
rev = rev.next;
23+
}
24+
25+
int[] arr = new int[list.size()];
26+
for (int i = 0, j=list.size() - 1; i < list.size(); i++,j--) {
27+
arr[i] = list.get(j);
28+
}
29+
30+
return arr;
31+
}
32+
33+
private ListNode reverse(ListNode head) {
34+
ListNode curr = head;
35+
ListNode prev = null;
36+
ListNode next = null;
37+
38+
while (curr != null) {
39+
next = curr.next;
40+
curr.next = prev;
41+
prev = curr;
42+
curr = next;
43+
}
44+
45+
return prev;
46+
}
47+
}

0 commit comments

Comments
 (0)