Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 458848d

Browse files
leetcode_cpp
Change-Id: I90cac0eb020fb8e1f9ba56d803cfe7f7f8ec2fcd
1 parent 7b54024 commit 458848d

16 files changed

+1379
-0
lines changed

‎cpp/leetcode/101.对称二叉树.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=cpp
3+
*
4+
* [101] 对称二叉树
5+
*
6+
* https://leetcode-cn.com/problems/symmetric-tree/description/
7+
*
8+
* algorithms
9+
* Easy (55.21%)
10+
* Likes: 1412
11+
* Dislikes: 0
12+
* Total Accepted: 337.9K
13+
* Total Submissions: 611.9K
14+
* Testcase Example: '[1,2,2,3,4,4,3]'
15+
*
16+
* 给定一个二叉树,检查它是否是镜像对称的。
17+
*
18+
*
19+
*
20+
* 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
21+
*
22+
* ⁠ 1
23+
* ⁠ / \
24+
* ⁠ 2 2
25+
* ⁠/ \ / \
26+
* 3 4 4 3
27+
*
28+
*
29+
*
30+
*
31+
* 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
32+
*
33+
* ⁠ 1
34+
* ⁠ / \
35+
* ⁠ 2 2
36+
* ⁠ \ \
37+
* ⁠ 3 3
38+
*
39+
*
40+
*
41+
*
42+
* 进阶:
43+
*
44+
* 你可以运用递归和迭代两种方法解决这个问题吗?
45+
*
46+
*/
47+
48+
// Definition for a binary tree node.
49+
struct TreeNode {
50+
int val;
51+
TreeNode *left;
52+
TreeNode *right;
53+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
54+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
55+
TreeNode(int x, TreeNode *left, TreeNode *right)
56+
: val(x), left(left), right(right) {}
57+
};
58+
59+
// @lc code=start
60+
class Solution {
61+
private:
62+
bool check(TreeNode *left, TreeNode *right) {
63+
if (left == nullptr && right == nullptr) {
64+
return true;
65+
}
66+
67+
if (left == nullptr || right == nullptr) {
68+
return false;
69+
}
70+
71+
return left->val == right->val && check(left->left, right->right) &&
72+
check(left->right, right->left);
73+
}
74+
75+
public:
76+
bool isSymmetric(TreeNode *root) {
77+
return root != nullptr && check(root->left, root->right);
78+
}
79+
};
80+
// @lc code=end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* @lc app=leetcode.cn id=102 lang=cpp
3+
*
4+
* [102] 二叉树的层序遍历
5+
*
6+
* https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (64.14%)
10+
* Likes: 895
11+
* Dislikes: 0
12+
* Total Accepted: 323.6K
13+
* Total Submissions: 504.5K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。
17+
* (即逐层地,从左到右访问所有节点)。
18+
*
19+
*
20+
*
21+
* 示例:
22+
* 二叉树:[3,9,20,null,null,15,7],
23+
*
24+
*
25+
* ⁠ 3
26+
* ⁠ / \
27+
* ⁠ 9 20
28+
* ⁠ / \
29+
* ⁠ 15 7
30+
*
31+
*
32+
* 返回其层序遍历结果:
33+
*
34+
*
35+
* [
36+
* ⁠ [3],
37+
* ⁠ [9,20],
38+
* ⁠ [15,7]
39+
* ]
40+
*
41+
*
42+
*/
43+
44+
#include <queue>
45+
#include <vector>
46+
47+
// Definition for a binary tree node.
48+
struct TreeNode {
49+
int val;
50+
TreeNode *left;
51+
TreeNode *right;
52+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
53+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
54+
TreeNode(int x, TreeNode *left, TreeNode *right)
55+
: val(x), left(left), right(right) {}
56+
};
57+
58+
// @lc code=start
59+
class Solution {
60+
public:
61+
std::vector<std::vector<int>> levelOrder(TreeNode *root) {
62+
std::vector<std::vector<int>> ret;
63+
if (root == nullptr) {
64+
return ret;
65+
}
66+
std::queue<TreeNode *> q;
67+
q.push(root);
68+
while (!q.empty()) {
69+
int batch = q.size();
70+
std::vector<int> level(batch);
71+
for (int i = 0; i < batch; i++) {
72+
TreeNode *node = q.front();
73+
q.pop();
74+
level[i] = node->val;
75+
if (node->left) {
76+
q.push(node->left);
77+
}
78+
if (node->right) {
79+
q.push(node->right);
80+
}
81+
}
82+
ret.push_back(level);
83+
}
84+
return ret;
85+
}
86+
};
87+
// @lc code=end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* @lc app=leetcode.cn id=105 lang=cpp
3+
*
4+
* [105] 从前序与中序遍历序列构造二叉树
5+
*
6+
* https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (71.39%)
10+
* Likes: 1873
11+
* Dislikes: 0
12+
* Total Accepted: 460.6K
13+
* Total Submissions: 645.1K
14+
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
15+
*
16+
* 给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历,
17+
* inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
18+
*
19+
*
20+
*
21+
* 示例 1:
22+
*
23+
*
24+
* 输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
25+
* 输出: [3,9,20,null,null,15,7]
26+
*
27+
*
28+
* 示例 2:
29+
*
30+
*
31+
* 输入: preorder = [-1], inorder = [-1]
32+
* 输出: [-1]
33+
*
34+
*
35+
*
36+
*
37+
* 提示:
38+
*
39+
*
40+
* 1 <= preorder.length <= 3000
41+
* inorder.length == preorder.length
42+
* -3000 <= preorder[i], inorder[i] <= 3000
43+
* preorder 和 inorder 均 无重复 元素
44+
* inorder 均出现在 preorder
45+
* preorder 保证 为二叉树的前序遍历序列
46+
* inorder 保证 为二叉树的中序遍历序列
47+
*
48+
*
49+
*/
50+
51+
#include <unordered_map>
52+
#include <vector>
53+
// Definition for a binary tree node.
54+
struct TreeNode {
55+
int val;
56+
TreeNode *left;
57+
TreeNode *right;
58+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
59+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
60+
TreeNode(int x, TreeNode *left, TreeNode *right)
61+
: val(x), left(left), right(right) {}
62+
};
63+
64+
// @lc code=start
65+
class Solution {
66+
private:
67+
std::unordered_map<int, int> inorder_index;
68+
TreeNode *buildTree(std::vector<int> &preorder, int pre_left, int pre_right,
69+
std::vector<int> &inorder, int in_left, int in_right) {
70+
if (pre_left > pre_right) {
71+
return nullptr;
72+
}
73+
int mid = inorder_index[preorder[pre_left]] - in_left; // count
74+
TreeNode *root = new TreeNode(preorder[pre_left]);
75+
root->left = buildTree(preorder, pre_left + 1, pre_left + mid, inorder,
76+
in_left, in_left + mid - 1);
77+
root->right = buildTree(preorder, pre_left + mid + 1, pre_right, inorder,
78+
in_left + mid + 1, in_right);
79+
return root;
80+
}
81+
82+
public:
83+
TreeNode *buildTree(std::vector<int> &preorder, std::vector<int> &inorder) {
84+
int n = inorder.size();
85+
for (int i = 0; i < n; i++) {
86+
inorder_index[inorder[i]] = i;
87+
}
88+
return buildTree(preorder, 0, n - 1, inorder, 0, n - 1);
89+
}
90+
};
91+
// @lc code=end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* @lc app=leetcode.cn id=121 lang=cpp
3+
*
4+
* [121] 买卖股票的最佳时机
5+
*
6+
* https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/
7+
*
8+
* algorithms
9+
* Easy (56.84%)
10+
* Likes: 1664
11+
* Dislikes: 0
12+
* Total Accepted: 463K
13+
* Total Submissions: 814.4K
14+
* Testcase Example: '[7,1,5,3,6,4]'
15+
*
16+
* 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i
17+
* 天的价格。
18+
*
19+
* 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子
20+
* 卖出该股票。设计一个算法来计算你所能获取的最大利润。
21+
*
22+
* 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
23+
*
24+
*
25+
*
26+
* 示例 1:
27+
*
28+
*
29+
* 输入:[7,1,5,3,6,4]
30+
* 输出:5
31+
* 解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 =
32+
* 6)的时候卖出,最大利润 = 6-1 = 5 。 ⁠ 注意利润不能是 7-1 = 6,
33+
* 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
*
39+
* 输入:prices = [7,6,4,3,1]
40+
* 输出:0
41+
* 解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
42+
*
43+
*
44+
*
45+
*
46+
* 提示:
47+
*
48+
*
49+
* 1
50+
* 0
51+
*
52+
*
53+
*/
54+
55+
#include <algorithm>
56+
#include <vector>
57+
58+
// @lc code=start
59+
class Solution {
60+
public:
61+
int maxProfit(std::vector<int> &prices) {
62+
if (prices.empty()) {
63+
return 0;
64+
}
65+
int buy = prices[0];
66+
int profit = 0;
67+
for (int i = 1; i < prices.size(); i++) {
68+
buy = std::min(buy, prices[i]);
69+
profit = std::max(profit, prices[i] - buy);
70+
}
71+
return profit;
72+
}
73+
};
74+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /