Skip to content

Commit 2630fb7

Browse files
committed
asdf
1 parent a81c405 commit 2630fb7

File tree

7 files changed

+183
-4
lines changed

7 files changed

+183
-4
lines changed

Diff for: README.md

+7-4
Large diffs are not rendered by default.

Diff for: my-submissions/m1008 Recursion.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Solution {
17+
public TreeNode bstFromPreorder(int[] preorder) {
18+
TreeNode root = new TreeNode(preorder[0]);
19+
bstHelper(preorder, root, 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
20+
21+
return root;
22+
}
23+
24+
private int bstHelper(int[] preorder, TreeNode curr, int indx, int lowerbound, int upperbound) {
25+
if (indx >= preorder.length) {
26+
return indx;
27+
}
28+
29+
if (lowerbound < preorder[indx] && preorder[indx] < curr.val) {
30+
curr.left = new TreeNode(preorder[indx]);
31+
indx = bstHelper(preorder, curr.left, indx + 1, lowerbound, curr.val);
32+
}
33+
34+
if (indx >= preorder.length) {
35+
return indx;
36+
}
37+
38+
if (preorder[indx] < upperbound && preorder[indx] > curr.val) {
39+
curr.right = new TreeNode(preorder[indx]);
40+
indx = bstHelper(preorder, curr.right, indx + 1, curr.val, upperbound);
41+
}
42+
43+
return indx;
44+
}
45+
}

Diff for: my-submissions/m1008.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
int bstHelper(int* preorder, int preorderSize, struct TreeNode* curr, int indx, int lowerbound, int upperbound) {
11+
if (indx >= preorderSize) {
12+
return indx;
13+
}
14+
15+
if (lowerbound < preorder[indx] && preorder[indx] < curr->val) {
16+
curr->left = (struct TreeNode*) malloc(sizeof(struct TreeNode));
17+
curr->left->val = preorder[indx];
18+
curr->left->left = NULL;
19+
curr->left->right = NULL;
20+
indx = bstHelper(preorder, preorderSize, curr->left, indx + 1, lowerbound, curr->val);
21+
}
22+
23+
if (indx >= preorderSize) {
24+
return indx;
25+
}
26+
27+
if (preorder[indx] < upperbound && preorder[indx] > curr->val) {
28+
curr->right = (struct TreeNode*) malloc(sizeof(struct TreeNode));
29+
curr->right->val = preorder[indx];
30+
curr->right->right = NULL;
31+
curr->right->left = NULL;
32+
indx = bstHelper(preorder, preorderSize, curr->right, indx + 1, curr->val, upperbound);
33+
}
34+
35+
return indx;
36+
}
37+
38+
struct TreeNode* bstFromPreorder(int* preorder, int preorderSize) {
39+
struct TreeNode* root = (struct TreeNode*) malloc(sizeof(struct TreeNode));
40+
root->val = preorder[0];
41+
root->left = NULL;
42+
root->right = NULL;
43+
bstHelper(preorder, preorderSize, root, 1, INT_MIN, INT_MAX);
44+
return root;
45+
}

Diff for: my-submissions/m1008.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* bstFromPreorder(vector<int>& preorder) {
15+
TreeNode* root = new TreeNode(preorder[0]);
16+
bstHelper(preorder, root, 1, INT_MIN, INT_MAX);
17+
return root;
18+
}
19+
20+
int bstHelper(vector<int>& preorder, TreeNode* curr, int indx, int lowerbound, int upperbound) {
21+
if (indx >= preorder.size()) {
22+
return indx;
23+
}
24+
25+
if (lowerbound < preorder[indx] && preorder[indx] < curr->val) {
26+
curr->left = new TreeNode(preorder[indx]);
27+
indx = bstHelper(preorder, curr->left, indx + 1, lowerbound, curr->val);
28+
}
29+
30+
if (indx >= preorder.size()) {
31+
return indx;
32+
}
33+
34+
if (preorder[indx] < upperbound && preorder[indx] > curr->val) {
35+
curr->right = new TreeNode(preorder[indx]);
36+
indx = bstHelper(preorder, curr->right, indx + 1, curr->val, upperbound);
37+
}
38+
39+
40+
return indx;
41+
}
42+
};

Diff for: my-submissions/m1852.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] distinctNumbers(int[] nums, int k) {
3+
int[] output = new int[nums.length - k + 1];
4+
HashMap<Integer, Integer> window = new HashMap<>();
5+
6+
for (int i = 0; i < k - 1; i++) {
7+
window.put(nums[i], window.getOrDefault(nums[i], 0) + 1);
8+
}
9+
10+
for (int i = k - 1; i < nums.length; i++) {
11+
window.put(nums[i], window.getOrDefault(nums[i], 0) + 1);
12+
13+
output[i - k + 1] = window.size();
14+
15+
if (window.get(nums[i - k + 1]) == 1) {
16+
window.remove(nums[i - k + 1]);
17+
} else {
18+
window.put(nums[i - k + 1], window.get(nums[i - k + 1]) - 1);
19+
}
20+
}
21+
22+
return output;
23+
}
24+
}

Diff for: my-submissions/m2221.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int triangularSum(int* nums, int numsSize) {
2+
for (int i = 0; i < numsSize; i++) {
3+
for (int j = 0; j < numsSize - i - 1; j++) {
4+
nums[j] = (nums[j] + nums[j + 1]) % 10;
5+
}
6+
}
7+
8+
return nums[0] % 10;
9+
}

Diff for: my-submissions/m2221.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int triangularSum(int[] nums) {
3+
for (int i = 0; i < nums.length; i++) {
4+
for (int j = 0; j < nums.length - i - 1; j++) {
5+
nums[j] = (nums[j] + nums[j + 1]) % 10;
6+
}
7+
}
8+
9+
return nums[0] % 10;
10+
}
11+
}

0 commit comments

Comments
 (0)