modify code on class

pull/6/head
左程云 4 years ago
parent 674086e1df
commit b579372ed7

@ -101,8 +101,10 @@ public class Code03_SmallerEqualBigger {
sT.next = eH; sT.next = eH;
eT = eT == null ? sT : eT; // 下一步谁去连大于区域的头谁就变成eT eT = eT == null ? sT : eT; // 下一步谁去连大于区域的头谁就变成eT
} }
// 上面的if不管跑了没有et // 下一步一定是需要用eT 去接 大于区域的头
// all reconnect // 有等于区域eT -> 等于区域的尾结点
// 无等于区域eT -> 小于区域的尾结点
// eT 尽量不为空的尾巴节点
if (eT != null) { // 如果小于区域和等于区域,不是都没有 if (eT != null) { // 如果小于区域和等于区域,不是都没有
eT.next = mH; eT.next = mH;
} }

@ -15,6 +15,8 @@ public class Code04_CopyListWithRandom {
} }
public static Node copyListWithRand1(Node head) { public static Node copyListWithRand1(Node head) {
// key 老节点
// value 新节点
HashMap<Node, Node> map = new HashMap<Node, Node>(); HashMap<Node, Node> map = new HashMap<Node, Node>();
Node cur = head; Node cur = head;
while (cur != null) { while (cur != null) {
@ -25,6 +27,7 @@ public class Code04_CopyListWithRandom {
while (cur != null) { while (cur != null) {
// cur 老 // cur 老
// map.get(cur) 新 // map.get(cur) 新
// 新.next -> cur.next克隆节点找到
map.get(cur).next = map.get(cur.next); map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand); map.get(cur).rand = map.get(cur.rand);
cur = cur.next; cur = cur.next;

@ -32,21 +32,22 @@ public class Code05_FindFirstIntersectNode {
return null; return null;
} }
// n1 慢 n2 快 // n1 慢 n2 快
Node n1 = head.next; // n1 -> slow Node slow = head.next; // n1 -> slow
Node n2 = head.next.next; // n2 -> fast Node fast = head.next.next; // n2 -> fast
while (n1 != n2) { while (slow != fast) {
if (n2.next == null || n2.next.next == null) { if (fast.next == null || fast.next.next == null) {
return null; return null;
} }
n2 = n2.next.next; fast = fast.next.next;
n1 = n1.next; slow = slow.next;
} }
n2 = head; // n2 -> walk again from head // slow fast 相遇
while (n1 != n2) { fast = head; // n2 -> walk again from head
n1 = n1.next; while (slow != fast) {
n2 = n2.next; slow = slow.next;
fast = fast.next;
} }
return n1; return slow;
} }
// 如果两个链表都无环返回第一个相交节点如果不想交返回null // 如果两个链表都无环返回第一个相交节点如果不想交返回null

@ -2,25 +2,4 @@ package class06;
public class Test { public class Test {
public static class Node{
public int value;
public Node next;
public Node(int v) {
value = v;
}
}
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
a.next = b;
b.next = c;
c = null;
}
} }

@ -33,18 +33,18 @@ public class Code02_UnRecursiveTraversalBT {
System.out.println(); System.out.println();
} }
public static void in(Node head) { public static void in(Node cur) {
System.out.print("in-order: "); System.out.print("in-order: ");
if (head != null) { if (cur != null) {
Stack<Node> stack = new Stack<Node>(); Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) { while (!stack.isEmpty() || cur != null) {
if (head != null) { if (cur != null) {
stack.push(head); stack.push(cur);
head = head.left; cur = cur.left;
} else { } else {
head = stack.pop(); cur = stack.pop();
System.out.print(head.value + " "); System.out.print(cur.value + " ");
head = head.right; cur = cur.right;
} }
} }
} }
@ -58,7 +58,7 @@ public class Code02_UnRecursiveTraversalBT {
Stack<Node> s2 = new Stack<Node>(); Stack<Node> s2 = new Stack<Node>();
s1.push(head); s1.push(head);
while (!s1.isEmpty()) { while (!s1.isEmpty()) {
head = s1.pop(); head = s1.pop(); // 头 右 左
s2.push(head); s2.push(head);
if (head.left != null) { if (head.left != null) {
s1.push(head.left); s1.push(head.left);
@ -67,6 +67,7 @@ public class Code02_UnRecursiveTraversalBT {
s1.push(head.right); s1.push(head.right);
} }
} }
// 左 右 头
while (!s2.isEmpty()) { while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " "); System.out.print(s2.pop().value + " ");
} }

Loading…
Cancel
Save