mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
更新代码块
This commit is contained in:
@@ -71,7 +71,7 @@ dp[1] = max(nums[0], nums[1]);
|
||||
|
||||
这道题目我给出了暴力的解法:
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
class Solution {
|
||||
public:
|
||||
int rob(TreeNode* root) {
|
||||
@@ -94,7 +94,7 @@ public:
|
||||
|
||||
代码如下:
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
class Solution {
|
||||
public:
|
||||
unordered_map<TreeNode* , int> umap; // 记录计算过的结果
|
||||
@@ -120,7 +120,7 @@ public:
|
||||
|
||||
1. 确定递归函数的参数和返回值
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
vector<int> robTree(TreeNode* cur) {
|
||||
```
|
||||
|
||||
@@ -142,7 +142,7 @@ if (cur == NULL) return vector<int>{0, 0};
|
||||
|
||||
采用后序遍历,代码如下:
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
// 下标0:不偷,下标1:偷
|
||||
vector<int> left = robTree(cur->left); // 左
|
||||
vector<int> right = robTree(cur->right); // 右
|
||||
@@ -160,7 +160,7 @@ vector<int> right = robTree(cur->right); // 右
|
||||
|
||||
代码如下:
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
vector<int> left = robTree(cur->left); // 左
|
||||
vector<int> right = robTree(cur->right); // 右
|
||||
|
||||
@@ -218,7 +218,7 @@ public:
|
||||
|
||||
因为股票就买卖一次,那么贪心的想法很自然就是取最左最小值,取最右最大值,那么得到的差值就是最大利润。
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
class Solution {
|
||||
public:
|
||||
int maxProfit(vector<int>& prices) {
|
||||
@@ -237,7 +237,7 @@ public:
|
||||
|
||||
动规解法,版本一,代码如下:
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
// 版本一
|
||||
class Solution {
|
||||
public:
|
||||
@@ -262,7 +262,7 @@ public:
|
||||
|
||||
那么我们只需要记录 当前天的dp状态和前一天的dp状态就可以了,可以使用滚动数组来节省空间,代码如下:
|
||||
|
||||
```C++
|
||||
```CPP
|
||||
// 版本二
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user