add
This commit is contained in:
parent
bfcc5da9f6
commit
5c1401ba7a
23
5.leetcode/src/com/fanxb/common/Q105.java
Normal file
23
5.leetcode/src/com/fanxb/common/Q105.java
Normal file
@ -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<Integer, Integer> 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<Integer, Integer> 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;
|
||||||
|
}
|
||||||
|
}
|
26
5.leetcode/src/com/fanxb/common/Q106.java
Normal file
26
5.leetcode/src/com/fanxb/common/Q106.java
Normal file
@ -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<Integer, Integer> 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<Integer, Integer> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
5.leetcode/src/com/fanxb/common/Q82.java
Normal file
45
5.leetcode/src/com/fanxb/common/Q82.java
Normal file
@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user