Compare commits
4 Commits
f3e576da31
...
bd744934a1
Author | SHA1 | Date | |
---|---|---|---|
bd744934a1 | |||
7bc26e591c | |||
d612697646 | |||
926b9fccff |
33
144_binary_tree_preorder_traversal/main.cpp
Normal file
33
144_binary_tree_preorder_traversal/main.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode *left;
|
||||||
|
TreeNode *right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> preorderTraversal(TreeNode *root) {
|
||||||
|
vector<int> result;
|
||||||
|
unordered_map<TreeNode *, bool> visited;
|
||||||
|
|
||||||
|
function<void(TreeNode *)> dfs = [&](TreeNode *root) {
|
||||||
|
if (!root) return;
|
||||||
|
if (visited[root]) return;
|
||||||
|
visited[root] = true;
|
||||||
|
|
||||||
|
result.emplace_back(root->val);
|
||||||
|
dfs(root->left);
|
||||||
|
dfs(root->right);
|
||||||
|
};
|
||||||
|
|
||||||
|
dfs(root);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
33
145_binary_tree_postorder_traversal/main.cpp
Normal file
33
145_binary_tree_postorder_traversal/main.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode *left;
|
||||||
|
TreeNode *right;
|
||||||
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<int> preorderTraversal(TreeNode *root) {
|
||||||
|
vector<int> result;
|
||||||
|
unordered_map<TreeNode *, bool> visited;
|
||||||
|
|
||||||
|
function<void(TreeNode *)> dfs = [&](TreeNode *root) {
|
||||||
|
if (!root) return;
|
||||||
|
if (visited[root]) return;
|
||||||
|
visited[root] = true;
|
||||||
|
|
||||||
|
dfs(root->left);
|
||||||
|
dfs(root->right);
|
||||||
|
result.emplace_back(root->val);
|
||||||
|
};
|
||||||
|
|
||||||
|
dfs(root);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
32
53_maximum_subarray/main.cpp
Normal file
32
53_maximum_subarray/main.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int maxSubArray(vector<int>& nums) {
|
||||||
|
return maxSubArray(nums, 0, nums.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxSubArray(vector<int>& nums, int leftIndex, int rightIndex) {
|
||||||
|
if (leftIndex > rightIndex) return INT32_MIN;
|
||||||
|
|
||||||
|
int mid = (leftIndex + rightIndex) / 2;
|
||||||
|
int leftMaxSum = 0, rightMaxSum = 0;
|
||||||
|
|
||||||
|
// left half side sum
|
||||||
|
for (int i = mid - 1, currentSum = 0; i >= leftIndex; i--) {
|
||||||
|
currentSum += nums[i];
|
||||||
|
leftMaxSum = max(leftMaxSum, currentSum);
|
||||||
|
}
|
||||||
|
|
||||||
|
// right half side sum
|
||||||
|
for (int i = mid + 1, currentSum = 0; i <= rightIndex; i++) {
|
||||||
|
currentSum += nums[i];
|
||||||
|
rightMaxSum = max(rightMaxSum, currentSum);
|
||||||
|
}
|
||||||
|
|
||||||
|
return max(max(maxSubArray(nums, leftIndex, mid - 1),
|
||||||
|
maxSubArray(nums, mid + 1, rightIndex)),
|
||||||
|
leftMaxSum + nums[mid] + rightMaxSum);
|
||||||
|
}
|
||||||
|
};
|
54
54_spiral_matrix/main.cpp
Normal file
54
54_spiral_matrix/main.cpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
// {row, col}
|
||||||
|
const vector<pair<int, int>> directions{
|
||||||
|
{0, 1},
|
||||||
|
{1, 0},
|
||||||
|
{0, -1},
|
||||||
|
{-1, 0},
|
||||||
|
};
|
||||||
|
|
||||||
|
inline pair<int, int> addPair(const pair<int, int>& x, const pair<int, int>& y) {
|
||||||
|
return {x.first + y.first, x.second + y.second};
|
||||||
|
}
|
||||||
|
|
||||||
|
void nextDirection(int& index, pair<int, int>& direction) {
|
||||||
|
if (++index >= 4) index = 0;
|
||||||
|
direction = directions[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
vector<int> spiralOrder(vector<vector<int>>& matrix) {
|
||||||
|
const int m = matrix.size();
|
||||||
|
const int n = matrix[0].size();
|
||||||
|
|
||||||
|
vector<int> result;
|
||||||
|
|
||||||
|
vector<vector<bool>> visited(m + 2, vector<bool>(n + 2, false));
|
||||||
|
for (int i = 0; i < m + 2; i++)
|
||||||
|
visited[i][0] = true, visited[i][n + 1] = true;
|
||||||
|
for (int i = 0; i < n + 2; i++)
|
||||||
|
visited[0][i] = true, visited[m + 1][i] = true;
|
||||||
|
|
||||||
|
int directionIndex = 0;
|
||||||
|
pair<int, int> direction = directions[directionIndex];
|
||||||
|
|
||||||
|
pair<int, int> currentPos{1, 1};
|
||||||
|
|
||||||
|
for (int i = 0; i < m * n; i++) {
|
||||||
|
auto nextPos = addPair(currentPos, direction);
|
||||||
|
if (visited[nextPos.first][nextPos.second]) {
|
||||||
|
nextDirection(directionIndex, direction);
|
||||||
|
nextPos = addPair(currentPos, direction);
|
||||||
|
}
|
||||||
|
visited[currentPos.first][currentPos.second] = true;
|
||||||
|
result.emplace_back(matrix[currentPos.first - 1][currentPos.second - 1]);
|
||||||
|
currentPos = nextPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user