From 5c1401ba7a684de0a51b8f6427bd02fc970984f9 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sun, 24 Mar 2024 07:47:43 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q105.java | 23 ++++++++++++ 5.leetcode/src/com/fanxb/common/Q106.java | 26 +++++++++++++ 5.leetcode/src/com/fanxb/common/Q82.java | 45 +++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q105.java create mode 100644 5.leetcode/src/com/fanxb/common/Q106.java create mode 100644 5.leetcode/src/com/fanxb/common/Q82.java diff --git a/5.leetcode/src/com/fanxb/common/Q105.java b/5.leetcode/src/com/fanxb/common/Q105.java new file mode 100644 index 0000000..02969d0 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q105.java @@ -0,0 +1,23 @@ +package com.fanxb.common; + +import java.util.HashMap; +import java.util.Map; + +public class Q105 { + public TreeNode buildTree(int[] preorder, int[] inorder) { + Map map = new HashMap<>(inorder.length); + for (int i = 0; i < inorder.length; i++) map.put(inorder[i], i); + return buildTree(preorder, map, 0, preorder.length - 1, 0, inorder.length - 1); + } + + public TreeNode buildTree(int[] preorder, Map map, int pL, int pR, int iL, int iR) { + if (pL > pR) return null; + TreeNode root = new TreeNode(preorder[pL]); + int rs = map.get(root.val); + //前序左边子树结束位置 + int newPr = pL + rs - iL; + root.left = buildTree(preorder, map, pL + 1, newPr, iL, rs - 1); + root.right = buildTree(preorder, map, newPr + 1, pR, rs + 1, iR); + return root; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q106.java b/5.leetcode/src/com/fanxb/common/Q106.java new file mode 100644 index 0000000..87898b6 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q106.java @@ -0,0 +1,26 @@ +package com.fanxb.common; + +import java.util.HashMap; +import java.util.Map; + +public class Q106 { + public TreeNode buildTree(int[] inorder, int[] postorder) { + Map map = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { + map.put(inorder[i], i); + } + return buildTree(postorder, map, 0, postorder.length - 1, 0, inorder.length - 1); + } + + private TreeNode buildTree(int[] pre, Map map, int pL, int pR, int iL, int iR) { + if (pL > pR || iL > iR) return null; + TreeNode root = new TreeNode(pre[pR]); + int index = map.get(pre[pR]); + System.out.println(index); + int newPr = pL + index - iL - 1; + root.left = buildTree(pre, map, pL, newPr, iL, index - 1); + root.right = buildTree(pre, map, newPr + 1, pR - 1, index + 1, iR); + return root; + } + +} diff --git a/5.leetcode/src/com/fanxb/common/Q82.java b/5.leetcode/src/com/fanxb/common/Q82.java new file mode 100644 index 0000000..6c5396a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q82.java @@ -0,0 +1,45 @@ +package com.fanxb.common; + +public class Q82 { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) return head; + ListNode res = new ListNode(), last = res, startBefore = null; + res.next = head; + while (head.next != null) { + if (head.val == head.next.val) { + if (startBefore == null) + startBefore = last; + last = head; + } else if (startBefore != null) { + last = startBefore; + startBefore.next = head.next; + startBefore = null; + } else { + last = head; + } + head = head.next; + } + if (startBefore != null) startBefore.next = null; + return res.next; + } + + public ListNode better(ListNode head) { + ListNode res = new ListNode(); + res.next = head; + ListNode cur = res.next, last = res; + while (cur != null && cur.next != null) { + if (cur.val == cur.next.val) { + int x = cur.next.val; + while (cur != null && cur.val == x) { + last.next = cur.next; + cur = cur.next; + } + } else { + last = cur; + cur = cur.next; + } + } + return res.next; + + } +}