当前位置:网站首页>每日升个级
每日升个级
2022-07-21 05:16:00 【原来只是一阵风】
总结个人力扣刷题记录:
STL容器的综合运用题https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/submissions/代码:
class Solution
{
public:
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>>res_vec;//存放结果的容器
if (!root)
{
return res_vec;
}
queue< TreeNode*>T_que; //利用队列的特行存放树的结点
T_que.push(root);
bool Inleft = true; //确定一个需要遍历左树的条件
while (!T_que.empty()) //只要容器内有元素那就一直循环
{
int len = T_que.size(); //获得队列的元素个数
deque<int>D_que;//利用双端数组的特效来遍历左右树
for (int i = 0; i < len; ++i)
{
TreeNode* temp = T_que.front(); //获取队头元素后出队
T_que.pop();
if (Inleft)//首先根据题目要求判断左右树
{
D_que.push_back(temp->val); //如果在左树的话那么就头插入
}
else
{
D_que.push_front(temp->val); //反之
}
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;
}
};
执行结果:
此题主要考虑去使用三个STL容器解决,可以加深对STL的理解,相信做完这道题,对STL的掌握度会小有提升
哈希表:141环形链表
礼物的最大价值https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/
这道题我觉得非常考验逻辑能力
题解:
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];
}
};
执行结果:
边栏推荐
猜你喜欢
234. Palindrome linked list
The evolution of data warehouse in recent 10 years and suggestions on database learning
Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
数据可视化应用安装部署 | 使用 datart 安装包和源码部署的常见问题教程
概率论-假设检验
004:打印字符
MySQL安装
What impact does the Internet of things have on the development of enterprises?
How to complete the design of RGB lamp Bluetooth mesh module from 0 to 1
2020普及组总结
随机推荐
Interviewer: have you made a judgment on the number of rows affected by your update?
226. Flip binary tree
概率论-方差和协方差、相关系数
029: Tao Tao picks apples
Teach you how to use yolov4 training and testing data set on the server (nanny level)
n的阶乘
Cookie快速入门
19. 删除链表的倒数第 N 个结点
1046. 最后一块石头的重量
What is a direct drinking machine? What is its working principle and advantages?
n factorial
What are the characteristics and application fields of Bluetooth module in the Internet of things?
In depth analysis of multiple knapsack problem (Part 1)
Linear regression finale (ridge, Lasso regression principle, formula derivation), you want everything here
05 unittest extension
Built in WiFi and Bluetooth chip of Flathead brother xuantie
2020 popularization group summary
Level 3 academic level test
844. 比较含退格的字符串
110. 平衡二叉树