Compare commits

..

No commits in common. "bd744934a169a53907117054e3e856b3b834db59" and "f3e576da31d909add5120985997373d100fce61a" have entirely different histories.

4 changed files with 0 additions and 152 deletions

View File

@ -1,33 +0,0 @@
#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;
}
};

View File

@ -1,33 +0,0 @@
#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;
}
};

View File

@ -1,32 +0,0 @@
#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);
}
};

View File

@ -1,54 +0,0 @@
#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;
}
};