From 32c54b4a5604aecab1681a84a8a58ed29b9a1151 Mon Sep 17 00:00:00 2001 From: Wayne <3522373084@qq.com> Date: Wed, 2 Mar 2022 16:03:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20968=20=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20Java=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81,=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0968.监控二叉树.md | 50 +++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index 433060a5..35c3ccdc 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -316,28 +316,44 @@ public: ### Java ```java class Solution { - private int count = 0; + int res=0; public int minCameraCover(TreeNode root) { - if (trval(root) == 0) count++; - return count; + // 对根节点的状态做检验,防止根节点是无覆盖状态 . + if(minCame(root)==0){ + res++; + } + return res; } - - private int trval(TreeNode root) { - if (root == null) return -1; - - int left = trval(root.left); - int right = trval(root.right); - - if (left == 0 || right == 0) { - count++; + /** + 节点的状态值: + 0 表示无覆盖 + 1 表示 有摄像头 + 2 表示有覆盖 + 后序遍历,根据左右节点的情况,来判读 自己的状态 + */ + public int minCame(TreeNode root){ + if(root==null){ + // 空节点默认为 有覆盖状态,避免在叶子节点上放摄像头 return 2; } - - if (left == 2 || right == 2) { + int left=minCame(root.left); + int right=minCame(root.right); + + // 如果左右节点都覆盖了的话, 那么本节点的状态就应该是无覆盖,没有摄像头 + if(left==2&&right==2){ + //(2,2) + return 0; + }else if(left==0||right==0){ + // 左右节点都是无覆盖状态,那 根节点此时应该放一个摄像头 + // (0,0) (0,1) (0,2) (1,0) (2,0) + // 状态值为 1 摄像头数 ++; + res++; return 1; + }else{ + // 左右节点的 状态为 (1,1) (1,2) (2,1) 也就是左右节点至少存在 1个摄像头, + // 那么本节点就是处于被覆盖状态 + return 2; } - - return 0; } } ``` @@ -391,7 +407,7 @@ class Solution: result += 1 return result -``` +``` ### Go ```go From d701e5e2aa80d44a24fbc54e241d6589e34d4b12 Mon Sep 17 00:00:00 2001 From: Wayne <3522373084@qq.com> Date: Sat, 5 Mar 2022 10:59:03 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9739=20=E6=AF=8F=E6=97=A5?= =?UTF-8?q?=E6=B8=A9=E5=BA=A6=E7=9A=84=20Java=20=E4=BB=A3=E7=A0=81,?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 76 +++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index d7489028..bdc75b96 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -177,34 +177,60 @@ public: Java: ```java -/** - * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到小 - *
- * 入站元素要和当前栈内栈首元素进行比较
- * 若大于栈首则 则与元素下标做差
- * 若大于等于则放入
- *
- * @param temperatures
- * @return
- */
- public static int[] dailyTemperatures(int[] temperatures) {
- Stack