Skip to content

Commit 0cda05b

Browse files
authored
Merge pull request #2344 from ys-han00/main
[ys-han00] WEEK 15 solutions
2 parents f0cca5b + 7574a7f commit 0cda05b

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
unordered_map<int, int> indices;
15+
int pre_idx = 0;
16+
17+
TreeNode* dfs(const vector<int>& preorder, int start, int end) {
18+
if (start > end)
19+
return nullptr;
20+
21+
int root_val = preorder[pre_idx++];
22+
TreeNode* root = new TreeNode(root_val);
23+
24+
int mid = indices[root_val];
25+
26+
root->left = dfs(preorder, start, mid - 1);
27+
root->right = dfs(preorder, mid + 1, end);
28+
29+
return root;
30+
}
31+
32+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
33+
for (int i = 0; i < inorder.size(); ++i)
34+
indices[inorder[i]] = i;
35+
36+
pre_idx = 0;
37+
38+
return dfs(preorder, 0, inorder.size() - 1);
39+
}
40+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
string longestPalindrome(string s) {
4+
int max_s = 0, max_e = 0;
5+
int n = s.size();
6+
vector<vector<bool>> dp(n, vector<bool>(n, false));
7+
8+
for(int i = n - 1; i >= 0; i--) {
9+
for(int j = i; j < n; j++) {
10+
if(i == j)
11+
dp[i][j] = true;
12+
else if(i + 1 == j)
13+
dp[i][j] = (s[i] == s[j]);
14+
else
15+
dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1];
16+
17+
if(dp[i][j] && max_e - max_s < j - i) {
18+
max_s = i;
19+
max_e = j;
20+
}
21+
}
22+
}
23+
24+
return s.substr(max_s, max_e - max_s + 1);
25+
}
26+
};

rotate-image/ys-han00.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
void rotate(vector<vector<int>>& matrix) {
4+
int top = 0, bottom = matrix.size() - 1;
5+
6+
while(top < bottom) {
7+
int left = top, right = bottom;
8+
for(int i = 0; i < bottom - top; i++) {
9+
int topLeft = matrix[top][left + i];
10+
matrix[top][left + i] = matrix[bottom - i][left];
11+
matrix[bottom - i][left] = matrix[bottom][right - i];
12+
matrix[bottom][right - i] = matrix[top + i][right];
13+
matrix[top + i][right] = topLeft;
14+
}
15+
top++;
16+
bottom--;
17+
}
18+
}
19+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
bool isSameTree(TreeNode* p, TreeNode* q) {
15+
if (p == nullptr && q == nullptr) return true;
16+
if (p == nullptr || q == nullptr) return false;
17+
if (p->val != q->val) return false;
18+
19+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
20+
}
21+
22+
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
23+
if (root == nullptr) return false;
24+
if (isSameTree(root, subRoot)) return true;
25+
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
26+
}
27+
28+
// bool ans = false;
29+
// void check(TreeNode* root, TreeNode* subRoot) {
30+
// if(root == nullptr || subRoot == nullptr || root->val != subRoot->val) {
31+
// if(root != nullptr || subRoot != nullptr)
32+
// ans = false;
33+
// return;
34+
// }
35+
// check(root->left, subRoot->left);
36+
// check(root->right, subRoot->right);
37+
// }
38+
39+
// void rec(TreeNode* root, TreeNode* subRoot) {
40+
// if(root == nullptr || ans)
41+
// return;
42+
// if(root->val == subRoot->val) {
43+
// ans = true;
44+
// check(root, subRoot);
45+
// if(ans) return;
46+
// }
47+
// rec(root->left, subRoot);
48+
// rec(root->right, subRoot);
49+
// }
50+
51+
// bool isSubtree(TreeNode* root, TreeNode* subRoot) {
52+
// rec(root, subRoot);
53+
// return ans;
54+
// }
55+
};

0 commit comments

Comments
 (0)