Skip to content

Commit

Permalink
[IM]: 算法刷题第一天
Browse files Browse the repository at this point in the history
  • Loading branch information
liujj committed Jul 9, 2022
1 parent 580b2d1 commit 3a75843
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 2 deletions.
53 changes: 51 additions & 2 deletions 03.code/08.leetcode/02.剑指offer2/29.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,47 @@
#include <algorithm>
using namespace std;
vector<int> spiralOrder(vector<vector<int> >& matrix) {

if (matrix.empty())
{
return {};
}
int left = 0; // 左边界
int right = matrix[0].size() - 1; // 右边界
int top = 0; // 上边界
int bottom = matrix.size() - 1; // 下边界

/*
------->------|
| |
| |
^ v
| |
|------<------|
*/
vector<int> ans;

while(true) {
// left -> right
for (int i = 0; i <= right; ++i) ans.push_back(matrix[top][i]);
++top;
if (top > bottom) break;

// top -> bottom
for (int i = top; i <= bottom; ++i) ans.push_back(matrix[i][right]);
--right;
if (right < left) break;

// right -> left
for (int i = right; i >= left; --i) ans.push_back(matrix[bottom][i]);
--bottom;
if (bottom < top) break;

// bottom -> top
for (int i = bottom; i >= top; --i) ans.push_back(matrix[i][left]);
++left;
if (left > right) break;
}
return ans;
}

int main() {
Expand All @@ -26,7 +65,17 @@ int main() {
matrix.push_back(a);
matrix.push_back(b);
matrix.push_back(c);
spiralOrder(matrix);
vector<int> ans = spiralOrder(matrix);

for (int i = 0; i < ans.size(); ++i)
{
// for (int j = 0; j < ans[i].size(); ++j)
// {
cout << ans[i] << " ";
// }

}
cout << endl;


return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int maxSubArray(vector<int>& nums) {
int pre = 0;
int maxAns = nums[0];
for (const auto &x : nums) {
pre = max(pre + x, x);
maxAns = max(maxAns, pre);
cout << "pre: " << pre << " ,maxAns: " << maxAns << endl;
}

return maxAns;
}

int main() {
vector<int> nums = {-2,1,-3,4,-1,2,1,-5,4};

cout << maxSubArray(nums) << endl;

return 0;
}
39 changes: 39 additions & 0 deletions 03.code/08.leetcode/02.剑指offer2/61扑克牌中的顺子.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
bool isStraight(vector<int>& nums) {
// set<int> ans;
// int zero = 0;

// for (int i : nums) {
// if (ans.find(i) != ans.end()) return false;
// if (i) ans.insert(i);

// }

// return ((*ans.rbegin() - *ans.begin()) < 5);

//用一个桶,用来计数
int bucket[14] = {0}; // 因为有14张牌
for (int num : nums){
//如果出现重复的牌,且该牌不是大小王0,则返回false。
if (num != 0 && bucket[num] > 0) return false;
//桶计数
bucket[num]++;
}
//用双指针定位到最大的牌和最小的牌
int left = 1;
int right = 13;
while (bucket[left] == 0) left++;
while (bucket[right] == 0) right--;
return right - left + 1 <= 5;
}

int main() {
vector<int> nums = {4,7,5,9,2};
// vector<vector<int> > num = {{1,2,3,4,5}, {123}};
cout << isStraight(nums) << endl;;
return 0;
}
65 changes: 65 additions & 0 deletions 03.code/08.leetcode/03.7.8刷题/111.二叉树的最小深度.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
// Definition for a binary tree node.
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) {}
};

int minDepth(TreeNode* root) {
// 深度优先 O(N)
if (nullptr == root) return 0;

if (root->left == nullptr && root->right == nullptr) {
return 1;
}
int min_depth = INT32_MAX;

if (nullptr != root->left) {
min_depth = min(minDepth(root->left), min_depth);
}

if (nullptr != root->right) {
min_depth = min(minDepth(root->right), min_depth);
}

return min_depth + 1;
}

int minDepth(TreeNode* root) {
if (nullptr == root) return 0;

std::queue<std::pair<TreeNode *, int>> que;
que.push(make_pair(root, 1));

while (!que.empty()) {
TreeNode *node = que.front().first;
int depth = que.front().second;
que.pop();

if (nullptr == node->left && nullptr == node->right) {
return depth;
}

if (nullptr != node->left) {
que.push(node->left, depth + 1);
}

id (nullptr != node->right) {
que.push(node->right, depth + 1);
}
}

return 0;
}
int main() {
// cout << min_depth << endl;

return 0;
}
55 changes: 55 additions & 0 deletions 03.code/08.leetcode/03.7.8刷题/226.翻转二叉树.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <queue>
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) {}
};

/*
TreeNode* invertTree(TreeNode* root) {
// 终止条件
if (nullptr == root) return nullptr;
// 左子树翻转
TreeNode *left = invertTree(root->left);
// 右子树翻转
TreeNode *right = invertTree(root->right);
root->right = left;
root->left = right;
return root;
}
*/

TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return nullptr;

queue<TreeNode *> que;
que.push(root);
while (!que.empty()) {
TreeNode *tmp = que.front();
que.pop();

TreeNode *left = tmp->left;
tmp->left = tmp->right;
tmp->right = tmp;
if (nullptr != tmp->left) que.push(tmp->left);
if (nullptr != tmp->right) que.push(tmp->right);
}

return root;
}

int main() {

TreeNode *root = invertTree(new TreeNode(4, new TreeNode(2), new TreeNode(7)));
cout << root->val << ", " << root->left->left << ", "<< root->right << endl;
return 0;
}

0 comments on commit 3a75843

Please sign in to comment.