当前位置:网站首页>Upgrade every day
Upgrade every day
2022-07-21 22:42:00 【It was just a gust of wind】
Summarize the record of personal effort deduction and question brushing :
class Solution
{
public:
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>>res_vec;// Container for storing results
if (!root)
{
return res_vec;
}
queue< TreeNode*>T_que; // Use the special line of the queue to store the nodes of the tree
T_que.push(root);
bool Inleft = true; // Determine a condition that requires traversing the left tree
while (!T_que.empty()) // As long as there are elements in the container, it keeps cycling
{
int len = T_que.size(); // Get the number of elements in the queue
deque<int>D_que;// Use the special effect of double ended array to traverse the left and right trees
for (int i = 0; i < len; ++i)
{
TreeNode* temp = T_que.front(); // Get the team head element and leave the team
T_que.pop();
if (Inleft)// First, judge the left and right trees according to the requirements of the topic
{
D_que.push_back(temp->val); // If it's in the left tree, then insert the head
}
else
{
D_que.push_front(temp->val); // conversely
}
if (temp->left) T_que.push(temp->left);
if (temp->right) T_que.push(temp->right);
}
res_vec.emplace_back(vector<int>{D_que.begin(), D_que.end()});
Inleft = !Inleft;
}
return res_vec;
}
};
Execution results :
This question mainly considers using three STL Container solution , Can deepen right STL The understanding of the , I believe I can finish this problem , Yes STL Your mastery will be slightly improved
Hashtable :141 Circular list
The greatest value of gifts https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/
I think this question is a great test of logical ability
Answer key :
class Solution {
public:
int maxValue(vector<vector<int>>& grid)
{
int hang = grid.size();
int lie = grid[0].size();
vector<vector<int>>dp(hang, vector<int>(lie, 0));
dp[0][0] = grid[0][0];
for (int j = 1; j < lie; ++j) dp[0][j] = dp[0][j - 1] + grid[0][j];
for (int i = 1; i < hang; ++i) dp[i][0] = dp[i - 1][0] + grid[i][0];
for (int z = 1; z < hang; ++z)
{
for (int x = 1; x < lie; ++x)
{
dp[z][x] = max(dp[z][x - 1], dp[z - 1][x]) + grid[z][x];
}
}
return dp[hang - 1][lie - 1];
}
};
Execution results :
边栏推荐
- 第二周ACM训练报告
- Smart devices are coming, making our lives more automated
- MySQL安装
- 小实操:实现Myarray
- Blockbuster: the domestic ide was released and developed by Alibaba. It is completely open source (high performance + high customization)
- STL初步了解
- ACM训练题解集记录
- 567. Arrangement of strings
- 每日升个级
- In depth analysis of multiple knapsack problem (Part 2)
猜你喜欢
Interviewer: have you made a judgment on the number of rows affected by your update?
226. 翻转二叉树
datart 自定义插件,不改动源代码,让 BI 顺利完成又一次创新
归并排序
110. 平衡二叉树
The evolution of data warehouse in recent 10 years and suggestions on database learning
QT初学者
数据可视化应用安装部署 | 使用 datart 安装包和源码部署的常见问题教程
Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
74. Search two-dimensional matrix
随机推荐
What opportunities and challenges does the development of instrument control panel face?
P1111 修复公路(并查集)
1046. Weight of the last stone
第九周ACM训练报告
What is a direct drinking machine? What is its working principle and advantages?
第一周ACM训练报告
好看又有趣的数据可视化图表制作,膜拜教程
第六周ACM训练报告
「跑象科技」获得天使+融资,打造新一代实时数据基础平台
438. 找到字符串中所有字母异位词
火爆社区的开源数据可视化工具 datart 新用户体验教程
The evolution of data warehouse in recent 10 years and suggestions on database learning
What is the Internet of things control system? What are its characteristics?
In depth analysis of multiple knapsack problem (Part 1)
005: storage space size of integer data type
234. 回文链表
438. Find all letter ectopic words in the string
Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
Verify Goldbach conjecture
437. Path sum III