Skip to content

Commit 6dfca92

Browse files
committed
🎨 Format files (🛠️ from Github Actions)
1 parent 8df902e commit 6dfca92

File tree

512 files changed

+8250
-7926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

512 files changed

+8250
-7926
lines changed

java/1-Two-Sum.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class Solution {
2-
public int[] twoSum(int[] nums, int target) {
3-
HashMap<Integer, Integer> prevMap = new HashMap<>();
4-
5-
for (int i = 0; i < nums.length; i++) {
6-
int num = nums[i];
7-
int diff = target - num;
8-
9-
if (prevMap.containsKey(nums[i])) {
10-
return new int[]{prevMap.get(num), i};
11-
}
12-
13-
prevMap.put(target - num, i);
14-
}
15-
16-
return new int[]{};
2+
3+
public int[] twoSum(int[] nums, int target) {
4+
HashMap<Integer, Integer> prevMap = new HashMap<>();
5+
6+
for (int i = 0; i < nums.length; i++) {
7+
int num = nums[i];
8+
int diff = target - num;
9+
10+
if (prevMap.containsKey(nums[i])) {
11+
return new int[] { prevMap.get(num), i };
12+
}
13+
14+
prevMap.put(target - num, i);
1715
}
18-
}
16+
17+
return new int[] {};
18+
}
19+
}

java/100-Same-Tree.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
* }
1717
*/
1818
class Solution {
19-
public boolean isSameTree(TreeNode p, TreeNode q) {
20-
return dfs(p, q);
19+
20+
public boolean isSameTree(TreeNode p, TreeNode q) {
21+
return dfs(p, q);
22+
}
23+
24+
private boolean dfs(TreeNode p, TreeNode q) {
25+
if (p == null && q == null) {
26+
return true;
2127
}
22-
23-
private boolean dfs(TreeNode p, TreeNode q) {
24-
if (p == null && q == null) {
25-
return true;
26-
}
27-
28-
if (p == null || q == null) {
29-
return false;
30-
}
31-
32-
if (p.val != q.val) return false;
33-
34-
boolean left = dfs(p.left, q.left);
35-
boolean right = dfs(p.right, q.right);
36-
37-
return left && right;
28+
29+
if (p == null || q == null) {
30+
return false;
3831
}
39-
}
4032

33+
if (p.val != q.val) return false;
34+
35+
boolean left = dfs(p.left, q.left);
36+
boolean right = dfs(p.right, q.right);
37+
38+
return left && right;
39+
}
40+
}

java/102-Binary-Tree-Level-Order-Traversal.java

+23-22
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,29 @@
1414
* }
1515
*/
1616
class Solution {
17-
public List<List<Integer>> levelOrder(TreeNode root) {
18-
List<List<Integer>> res = new ArrayList<>();
19-
Queue<TreeNode> queue = new LinkedList<>();
20-
21-
if (root == null) return res;
22-
23-
queue.add(root);
24-
while(!queue.isEmpty()) {
25-
int len = queue.size();
26-
List<Integer> level = new ArrayList<>();
27-
for (int i = 0; i < len; i++) {
28-
TreeNode curr = queue.poll();
29-
level.add(curr.val);
30-
if (curr.left != null) {
31-
queue.add(curr.left);
32-
}
33-
if (curr.right != null) {
34-
queue.add(curr.right);
35-
}
36-
}
37-
res.add(level);
17+
18+
public List<List<Integer>> levelOrder(TreeNode root) {
19+
List<List<Integer>> res = new ArrayList<>();
20+
Queue<TreeNode> queue = new LinkedList<>();
21+
22+
if (root == null) return res;
23+
24+
queue.add(root);
25+
while (!queue.isEmpty()) {
26+
int len = queue.size();
27+
List<Integer> level = new ArrayList<>();
28+
for (int i = 0; i < len; i++) {
29+
TreeNode curr = queue.poll();
30+
level.add(curr.val);
31+
if (curr.left != null) {
32+
queue.add(curr.left);
3833
}
39-
return res;
34+
if (curr.right != null) {
35+
queue.add(curr.right);
36+
}
37+
}
38+
res.add(level);
4039
}
40+
return res;
41+
}
4142
}
+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution {
2-
public int maxDepth(TreeNode root) {
3-
if (root == null) return 0;
4-
return 1+Math.max(maxDepth(root.left), maxDepth(root.right));
5-
}
2+
3+
public int maxDepth(TreeNode root) {
4+
if (root == null) return 0;
5+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
6+
}
67
}

java/1046-Last-Stone-Weight.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
2-
public int lastStoneWeight(int[] stones) {
3-
PriorityQueue<Integer> maxHeap = new PriorityQueue();
4-
for (int stone: stones)
5-
maxHeap.add(-stone);
6-
while (maxHeap.size()>1) {
7-
int stone1 = maxHeap.remove();
8-
int stone2 = maxHeap.remove();
9-
if (stone1!=stone2)
10-
maxHeap.add(stone1-stone2);
11-
}
12-
return maxHeap.size()!=0?(-maxHeap.remove()):0;
2+
3+
public int lastStoneWeight(int[] stones) {
4+
PriorityQueue<Integer> maxHeap = new PriorityQueue();
5+
for (int stone : stones) maxHeap.add(-stone);
6+
while (maxHeap.size() > 1) {
7+
int stone1 = maxHeap.remove();
8+
int stone2 = maxHeap.remove();
9+
if (stone1 != stone2) maxHeap.add(stone1 - stone2);
1310
}
11+
return maxHeap.size() != 0 ? (-maxHeap.remove()) : 0;
12+
}
1413
}

java/105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.java

+52-31
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,69 @@
1414
* }
1515
*/
1616
class Solution {
17-
public TreeNode buildTree(int[] preorder, int[] inorder) {
18-
if (preorder.length == 0 || inorder.length == 0) return null;
19-
20-
TreeNode root = new TreeNode(preorder[0]);
21-
int mid = 0;
22-
for (int i = 0; i < inorder.length; i++) {
23-
if (preorder[0] == inorder[i]) mid = i;
24-
}
25-
26-
root.left = buildTree(Arrays.copyOfRange(preorder, 1, mid + 1), Arrays.copyOfRange(inorder, 0, mid));
27-
root.right = buildTree(Arrays.copyOfRange(preorder, mid + 1, preorder.length), Arrays.copyOfRange(inorder, mid + 1, inorder.length));
28-
29-
return root;
17+
18+
public TreeNode buildTree(int[] preorder, int[] inorder) {
19+
if (preorder.length == 0 || inorder.length == 0) return null;
20+
21+
TreeNode root = new TreeNode(preorder[0]);
22+
int mid = 0;
23+
for (int i = 0; i < inorder.length; i++) {
24+
if (preorder[0] == inorder[i]) mid = i;
3025
}
26+
27+
root.left =
28+
buildTree(
29+
Arrays.copyOfRange(preorder, 1, mid + 1),
30+
Arrays.copyOfRange(inorder, 0, mid)
31+
);
32+
root.right =
33+
buildTree(
34+
Arrays.copyOfRange(preorder, mid + 1, preorder.length),
35+
Arrays.copyOfRange(inorder, mid + 1, inorder.length)
36+
);
37+
38+
return root;
39+
}
3140
}
3241

3342
// Solution without using Array copies
3443
class Solution {
3544

36-
Map<Integer, Integer> inorderPositions = new HashMap<>();
45+
Map<Integer, Integer> inorderPositions = new HashMap<>();
3746

38-
public TreeNode buildTree(int[] preorder, int[] inorder) {
39-
if(preorder.length < 1 || inorder.length < 1) return null;
47+
public TreeNode buildTree(int[] preorder, int[] inorder) {
48+
if (preorder.length < 1 || inorder.length < 1) return null;
4049

41-
for(int i = 0; i < inorder.length; i ++) {
42-
inorderPositions.put(inorder[i], i);
43-
}
44-
45-
return builder(preorder, 0, 0, inorder.length-1);
50+
for (int i = 0; i < inorder.length; i++) {
51+
inorderPositions.put(inorder[i], i);
4652
}
4753

48-
public TreeNode builder(int[] preorder, int preorderIndex, int inorderLow, int inorderHigh) {
49-
if (preorderIndex > preorder.length-1 || inorderLow > inorderHigh) return null;
54+
return builder(preorder, 0, 0, inorder.length - 1);
55+
}
5056

51-
int currentVal = preorder[preorderIndex];
52-
TreeNode n = new TreeNode(currentVal);
53-
int mid = inorderPositions.get(currentVal);
57+
public TreeNode builder(
58+
int[] preorder,
59+
int preorderIndex,
60+
int inorderLow,
61+
int inorderHigh
62+
) {
63+
if (
64+
preorderIndex > preorder.length - 1 || inorderLow > inorderHigh
65+
) return null;
5466

55-
n.left = builder(preorder, preorderIndex+1, inorderLow, mid-1);
56-
n.right = builder(preorder, preorderIndex + (mid - inorderLow) + 1, mid+1, inorderHigh);
67+
int currentVal = preorder[preorderIndex];
68+
TreeNode n = new TreeNode(currentVal);
69+
int mid = inorderPositions.get(currentVal);
5770

58-
return n;
59-
}
71+
n.left = builder(preorder, preorderIndex + 1, inorderLow, mid - 1);
72+
n.right =
73+
builder(
74+
preorder,
75+
preorderIndex + (mid - inorderLow) + 1,
76+
mid + 1,
77+
inorderHigh
78+
);
6079

61-
}
80+
return n;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,58 @@
11
//This video helped https://www.youtube.com/watch?v=LgLRTaEMRVc&ab_channel=takeUforward
22

33
class Solution {
4-
public TreeNode buildTree(int[] inorder, int[] postorder) {
5-
HashMap<Integer, Integer> inMap = new HashMap<>();
6-
for (int i = 0; i<inorder.length; i++) {
7-
inMap.put(inorder[i],i);
8-
}
9-
return helper(inorder, 0, inorder.length-1, postorder,0, postorder.length-1, inMap);
4+
5+
public TreeNode buildTree(int[] inorder, int[] postorder) {
6+
HashMap<Integer, Integer> inMap = new HashMap<>();
7+
for (int i = 0; i < inorder.length; i++) {
8+
inMap.put(inorder[i], i);
109
}
11-
12-
public TreeNode helper(int[] inorder, int iStart, int iEnd, int[] postorder, int pStart, int pEnd, HashMap<Integer, Integer> inMap) {
13-
if (pStart>pEnd || iStart>iEnd) {
14-
return null;
15-
}
16-
TreeNode root = new TreeNode(postorder[pEnd]);
17-
int index = inMap.get(postorder[pEnd]);
18-
int numsLeft = index-iStart;
19-
root.left = helper(inorder, iStart, index-1, postorder, pStart, pStart+numsLeft-1, inMap);
20-
root.right = helper(inorder, index+1, iEnd, postorder, pStart+numsLeft, pEnd-1, inMap);
21-
return root;
10+
return helper(
11+
inorder,
12+
0,
13+
inorder.length - 1,
14+
postorder,
15+
0,
16+
postorder.length - 1,
17+
inMap
18+
);
19+
}
20+
21+
public TreeNode helper(
22+
int[] inorder,
23+
int iStart,
24+
int iEnd,
25+
int[] postorder,
26+
int pStart,
27+
int pEnd,
28+
HashMap<Integer, Integer> inMap
29+
) {
30+
if (pStart > pEnd || iStart > iEnd) {
31+
return null;
2232
}
23-
33+
TreeNode root = new TreeNode(postorder[pEnd]);
34+
int index = inMap.get(postorder[pEnd]);
35+
int numsLeft = index - iStart;
36+
root.left =
37+
helper(
38+
inorder,
39+
iStart,
40+
index - 1,
41+
postorder,
42+
pStart,
43+
pStart + numsLeft - 1,
44+
inMap
45+
);
46+
root.right =
47+
helper(
48+
inorder,
49+
index + 1,
50+
iEnd,
51+
postorder,
52+
pStart + numsLeft,
53+
pEnd - 1,
54+
inMap
55+
);
56+
return root;
57+
}
2458
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
//Same just reverse in the end.
1+
//Same just reverse in the end.
22
//Reversing in the end is better than using add(0, E) in case of arraylist as it's an O(1) operation.
33

44
class Solution {
5-
public List<List<Integer>> levelOrderBottom(TreeNode root) {
6-
List<List<Integer>> ans = new ArrayList<>();
7-
if (root == null) return ans;
8-
Queue<TreeNode> q = new LinkedList<>();
9-
q.offer(root);
10-
while (!q.isEmpty()) {
11-
List<Integer> level = new ArrayList<>();
12-
int size = q.size();
13-
for (int i = 0; i<size; i++) {
14-
TreeNode cur = q.poll();
15-
level.add(cur.val);
16-
if (cur.left!=null) q.offer(cur.left);
17-
if (cur.right!=null) q.offer(cur.right);
18-
}
19-
ans.add(level);
20-
}
21-
int i = 0 , j = ans.size()-1;
22-
//reverse the list
23-
Collections.reverse(ans);
24-
return ans;
5+
6+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
7+
List<List<Integer>> ans = new ArrayList<>();
8+
if (root == null) return ans;
9+
Queue<TreeNode> q = new LinkedList<>();
10+
q.offer(root);
11+
while (!q.isEmpty()) {
12+
List<Integer> level = new ArrayList<>();
13+
int size = q.size();
14+
for (int i = 0; i < size; i++) {
15+
TreeNode cur = q.poll();
16+
level.add(cur.val);
17+
if (cur.left != null) q.offer(cur.left);
18+
if (cur.right != null) q.offer(cur.right);
19+
}
20+
ans.add(level);
2521
}
22+
int i = 0, j = ans.size() - 1;
23+
//reverse the list
24+
Collections.reverse(ans);
25+
return ans;
26+
}
2627
}

0 commit comments

Comments
 (0)