From d9b00d6d15899a969757976584a31ecc71e13535 Mon Sep 17 00:00:00 2001 From: whaifree Date: Wed, 20 Nov 2024 20:03:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(interview):=20=E6=B7=BB=E5=8A=A0=E6=AF=8F?= =?UTF-8?q?=E6=97=A5=E6=B8=A9=E5=BA=A6=E5=92=8C=E5=BE=AA=E7=8E=AF=E7=A7=BB?= =?UTF-8?q?=E4=BD=8D=E4=BA=8C=E5=8F=89=E6=A0=91=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88-=20=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=AF=8F=E6=97=A5?= =?UTF-8?q?=E6=B8=A9=E5=BA=A6=E9=97=AE=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=96=B9=E6=A1=88=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E6=A0=88=E7=AE=97=E6=B3=95=20-=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E7=A7=BB=E4=BD=8D=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= =?UTF-8?q?=EF=BC=8C=E9=87=87=E7=94=A8=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20-=20=E6=96=B0=E5=A2=9E=20P1=20=E7=B1=BB?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E6=AF=8F=E6=97=A5=E6=B8=A9=E5=BA=A6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=B3=95=20-=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Solution=20=E7=B1=BB=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E7=A7=BB=E4=BD=8D=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/whaifree/interview/Dws/P1.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 ForJdk17/src/main/java/cn/whaifree/interview/Dws/P1.java diff --git a/ForJdk17/src/main/java/cn/whaifree/interview/Dws/P1.java b/ForJdk17/src/main/java/cn/whaifree/interview/Dws/P1.java new file mode 100644 index 0000000..b019d45 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/interview/Dws/P1.java @@ -0,0 +1,121 @@ +package cn.whaifree.interview.Dws; + +import cn.whaifree.leetCode.model.TreeNode; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/20 19:32 + * @注释 + */ +public class P1 { + + class Solution { + /** + * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 + * + * 每日温度 + * @param dailyTemperatures int整型一维数组 + * @return int整型一维数组 + */ + public int[] temperatures (int[] dailyTemperatures) { + // write code here + // 每一天需要等待几天会出现更高温 + // 单调栈 + int[] res = new int[dailyTemperatures.length]; + Deque stack = new LinkedList<>(); + for (int i = 0; i < dailyTemperatures.length; i++) { + while (!stack.isEmpty()&&dailyTemperatures[i] > dailyTemperatures[stack.peek()]) { + Integer pop = stack.pop(); + res[pop] = i -pop; + } + stack.push(i); + } + return res; + } + } + + +} + +class Solution { + + static class Node{ + int parentIndex; + int currentIndex; + int flag; + TreeNode currentNode; + + public Node(int parentIndex, int currentIndex, int flag, TreeNode currentNode) { + this.parentIndex = parentIndex; + this.currentIndex = currentIndex; + this.flag = flag; + this.currentNode = currentNode; + } + } + /** + * https://blog.csdn.net/ouyang_peng/article/details/143254632 + * + * + * @param root TreeNode类 + * @param k int整型 + * @return TreeNode类 + */ + public TreeNode cyclicShiftTree (TreeNode root, int k) { + // write code here + + List> levels = new ArrayList<>(); + Queue queue = new LinkedList<>(); + + queue.offer(new Node(0,0,0,root)); + int parentNum = 1; + int nextNum = 0; + List tempLevel = new ArrayList<>(); + + while (!queue.isEmpty()){ + Node node = queue.poll(); + tempLevel.add(node); + parentNum--; + if (node.currentNode.left!=null){ + queue.offer(new Node(node.currentIndex,nextNum++,0,node.currentNode.left)); + } + if (node.currentNode.right!=null){ + queue.offer(new Node(node.currentIndex,nextNum++,1,node.currentNode.right)); + } + if (parentNum==0){ + parentNum = nextNum; + nextNum = 0; + levels.add(tempLevel); + tempLevel = new ArrayList<>(); + } + } + + int depth = levels.size()-1; + for (int i = depth; i > 0; i--) { + List parentLev = levels.get(i - 1); + int parentSize = parentLev.size(); + + for (Node node : parentLev) { + node.currentNode.left=null; + node.currentNode.right=null; + } + + int move = k%(2*parentSize); + tempLevel = levels.get(i); + for (Node node : tempLevel) { + int targetIndex = node.flag == 0 ? (node.parentIndex + move / 2) % parentSize : (node.parentIndex + (move + 1) / 2) % parentSize; + int targetFlag = node.flag == 0?move%2:(move+1); + Node targetP = parentLev.get(targetIndex); + if (targetFlag==0){ + targetP.currentNode.left = node.currentNode; + }else { + targetP.currentNode.right = node.currentNode; + } + } + } + + return root; + } +}