mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1131 from yp2020/master
修改 968 监控二叉树的 Java 代码 , 并增加注释
This commit is contained in:
@ -177,34 +177,60 @@ public:
|
||||
|
||||
Java:
|
||||
```java
|
||||
/**
|
||||
* 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到小
|
||||
* <p>
|
||||
* 入站元素要和当前栈内栈首元素进行比较
|
||||
* 若大于栈首则 则与元素下标做差
|
||||
* 若大于等于则放入
|
||||
*
|
||||
* @param temperatures
|
||||
* @return
|
||||
*/
|
||||
public static int[] dailyTemperatures(int[] temperatures) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int[] res = new int[temperatures.length];
|
||||
for (int i = 0; i < temperatures.length; i++) {
|
||||
|
||||
class Solution {
|
||||
// 版本 1
|
||||
public int[] dailyTemperatures(int[] temperatures) {
|
||||
|
||||
int lens=temperatures.length;
|
||||
int []res=new int[lens];
|
||||
|
||||
/**
|
||||
* 取出下标进行元素值的比较
|
||||
如果当前遍历的元素 大于栈顶元素,表示 栈顶元素的 右边的最大的元素就是 当前遍历的元素,
|
||||
所以弹出 栈顶元素,并记录
|
||||
如果栈不空的话,还要考虑新的栈顶与当前元素的大小关系
|
||||
否则的话,可以直接入栈。
|
||||
注意,单调栈里 加入的元素是 下标。
|
||||
*/
|
||||
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
|
||||
int preIndex = stack.pop();
|
||||
res[preIndex] = i - preIndex;
|
||||
Stack<Integer>stack=new Stack<>();
|
||||
stack.push(0);
|
||||
for(int i=1;i<lens;i++){
|
||||
|
||||
if(temperatures[i]<=temperatures[stack.peek()]){
|
||||
stack.push(i);
|
||||
}else{
|
||||
while(!stack.isEmpty()&&temperatures[i]>temperatures[stack.peek()]){
|
||||
res[stack.peek()]=i-stack.peek();
|
||||
stack.pop();
|
||||
}
|
||||
/**
|
||||
* 注意 放入的是元素位置
|
||||
*/
|
||||
stack.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
//--------这 是一条分界线
|
||||
// 版本 2
|
||||
class Solution {
|
||||
public int[] dailyTemperatures(int[] temperatures) {
|
||||
int lens=temperatures.length;
|
||||
int []res=new int[lens];
|
||||
Stack<Integer>stack=new Stack<>();
|
||||
for(int i=0;i<lens;i++){
|
||||
|
||||
while(!stack.isEmpty()&&temperatures[i]>temperatures[stack.peek()]){
|
||||
res[stack.peek()]=i-stack.peek();
|
||||
stack.pop();
|
||||
}
|
||||
stack.push(i);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
Python:
|
||||
``` Python3
|
||||
|
@ -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++;
|
||||
}
|
||||
|
||||
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++;
|
||||
return res;
|
||||
}
|
||||
/**
|
||||
节点的状态值:
|
||||
0 表示无覆盖
|
||||
1 表示 有摄像头
|
||||
2 表示有覆盖
|
||||
后序遍历,根据左右节点的情况,来判读 自己的状态
|
||||
*/
|
||||
public int minCame(TreeNode root){
|
||||
if(root==null){
|
||||
// 空节点默认为 有覆盖状态,避免在叶子节点上放摄像头
|
||||
return 2;
|
||||
}
|
||||
int left=minCame(root.left);
|
||||
int right=minCame(root.right);
|
||||
|
||||
if (left == 2 || right == 2) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 如果左右节点都覆盖了的话, 那么本节点的状态就应该是无覆盖,没有摄像头
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user