Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions problems/0376.摆动序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,23 @@ class Solution:
**贪心**
```golang
func wiggleMaxLength(nums []int) int {
var count, preDiff, curDiff int //初始化默认为0
count = 1 // 初始化为1,因为最小的序列是1个数
if len(nums) < 2 {
return count
n := len(nums)
if n < 2 {
return n
}
ans := 1
prevDiff := nums[1] - nums[0]
if prevDiff != 0 {
ans = 2
}
for i := 0; i < len(nums)-1; i++ {
curDiff = nums[i+1] - nums[i]
if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) {
count++
for i := 2; i < n; i++ {
diff := nums[i] - nums[i-1]
if diff > 0 && prevDiff <= 0 || diff < 0 && prevDiff >= 0 {
ans++
prevDiff = diff
}
}
return count
return ans
}
```

Expand Down
29 changes: 23 additions & 6 deletions problems/0746.使用最小花费爬楼梯.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

所以初始化代码为:

```
```CPP
vector<int> dp(cost.size());
dp[0] = cost[0];
dp[1] = cost[1];
Expand Down Expand Up @@ -201,15 +201,32 @@ public:


### Java

```Java
// 方式一:第一步支付费用
class Solution {
public int minCostClimbingStairs(int[] cost) {
if (cost == null || cost.length == 0) {
return 0;
}
if (cost.length == 1) {
return cost[0];
int len = cost.length;
int[] dp = new int[len + 1];

// 从下标为 0 或下标为 1 的台阶开始,因此支付费用为0
dp[0] = 0;
dp[1] = 0;

// 计算到达每一层台阶的最小费用
for (int i = 2; i <= len; i++) {
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}

return dp[len];
}
}
```

```Java
// 方式二:第一步不支付费用
class Solution {
public int minCostClimbingStairs(int[] cost) {
int[] dp = new int[cost.length];
dp[0] = cost[0];
dp[1] = cost[1];
Expand Down
12 changes: 6 additions & 6 deletions problems/0968.监控二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

后序遍历代码如下:

```
```CPP
int traversal(TreeNode* cur) {

// 空节点,该节点有覆盖
Expand Down Expand Up @@ -124,7 +124,7 @@ int traversal(TreeNode* cur) {

代码如下:

```
```CPP
// 空节点,该节点有覆盖
if (cur == NULL) return 2;
```
Expand All @@ -143,7 +143,7 @@ if (cur == NULL) return 2;

代码如下:

```
```CPP
// 左右节点都有覆盖
if (left == 2 && right == 2) return 0;
```
Expand All @@ -163,7 +163,7 @@ left == 2 && right == 0 左节点覆盖,右节点无覆盖
此时摄像头的数量要加一,并且return 1,代表中间节点放摄像头。

代码如下:
```
```CPP
if (left == 0 || right == 0) {
result++;
return 1;
Expand All @@ -180,7 +180,7 @@ left == 1 && right == 1 左右节点都有摄像头

代码如下:

```
```CPP
if (left == 1 || right == 1) return 2;
```

Expand All @@ -198,7 +198,7 @@ if (left == 1 || right == 1) return 2;

所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下:

```
```CPP
int minCameraCover(TreeNode* root) {
result = 0;
if (traversal(root) == 0) { // root 无覆盖
Expand Down