第36天--算法(牛客网剑指offer3)
1.JZ23 链表中环的入口结点
public ListNode EntryNodeOfLoop(ListNode pHead) {
if(pHead == null || pHead.next == null || pHead.next.next == null) {
return null;
}
ListNode fast = pHead.next.next;
ListNode slow = pHead.next;
while(fast != slow) {
if(fast.next.next == null || slow.next == null) {
return null;
}
fast = fast.next.next;
slow = slow.next;
}
fast = pHead;
while(fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
2.JZ24 反转链表
public ListNode ReverseList(ListNode head) {
ListNode res = new ListNode(0);
while(head != null) {
ListNode temp = head;
head = head.next;
temp.next = res.next;
res.next = temp;
}
return res.next;
}
3.JZ25 合并两个排序的链表
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode res = new ListNode(0);
ListNode cur = res;
while(list1 != null && list2 != null) {
if(list1.val <= list2.val) {
cur.next = list1;
list1 = list1.next;
cur = cur.next;
}else {
cur.next = list2;
list2 = list2.next;
cur = cur.next;
}
}
if(list1 != null) {
cur.next = list1;
}
if(list2 != null) {
cur.next = list2;
}
return res.next;
}
4.JZ26 树的子结构
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1 == null || root2 == null) {
return false;
}
return process(root1,root2) || HasSubtree(root1.left,root2) || HasSubtree(root1.right,root2);
}
public boolean process(TreeNode root1,TreeNode root2) {
if(root2 == null) {
return true;
}
if(root1 == null) {
return false;
}
return root1.val == root2.val && process(root1.left,root2.left) && process(root1.right,root2.right);
}
5.JZ27 二叉树的镜像
public TreeNode Mirror (TreeNode pRoot) {
if(pRoot == null) {
return null;
}
TreeNode temp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = temp;
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
6.JZ28 对称的二叉树
public boolean isSymmetrical(TreeNode pRoot) {
if(pRoot == null) {
return true;
}
return isMirror(pRoot,pRoot);
}
public boolean isMirror(TreeNode t1,TreeNode t2) {
if(t1 == null && t2 == null) {
return true;
}
if(t1 != null && t2 != null) {
return t1.val == t2.val && isMirror(t1.left,t2.right) && isMirror(t1.right,t2.left);
}
return false;
}
7.JZ29 顺时针打印矩阵
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> res = new ArrayList<>();
if(matrix == null || matrix.length == 0) {
return res;
}
int tR = 0;
int tC = 0;
int dR = matrix.length - 1;
int dC = matrix[0].length - 1;
while(tR <= dR && tC <= dC) {
process(res,matrix,tR ++,tC ++,dR --,dC --);
}
return res;
}
public void process(ArrayList<Integer> res,int [][] matrix,int tR,int tC,int dR,int dC) {
if(tR == dR) {
for(int i = tC;i <= dC;i ++) {
res.add(matrix[tR][i]);
}
return;
}
if(tC == dC) {
for(int i = tR;i <= dR;i ++) {
res.add(matrix[i][tC]);
}
return;
}
int curR = tR;
int curC = tC;
while(curC != dC) {
res.add(matrix[tR][curC ++]);
}
while(curR != dR) {
res.add(matrix[curR ++][dC]);
}
while(curC != tC) {
res.add(matrix[dR][curC --]);
}
while(curR != tR) {
res.add(matrix[curR --][tC]);
}
}
8.JZ30 包含min函数的栈
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
public void push(int node) {
s1.push(node);
if(s2.isEmpty()) {
s2.push(node);
}else {
s2.push(node < s2.peek() ? node : s2.peek());
}
}
public void pop() {
s1.pop();
s2.pop();
}
public int top() {
return s1.peek();
}
public int min() {
return s2.peek();
}
9.JZ31 栈的压入、弹出序列
public boolean IsPopOrder(int [] pushA,int [] popA) {
int size = 0;
int j = 0;
for(int num : pushA) {
pushA[size] = num;
while(size >= 0 && popA[j] == pushA[size]) {
size --;
j ++;
}
size ++;
}
return size == 0;
}
10.JZ32 从上往下打印二叉树
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
if(root == null) {
return res;
}
LinkedList<TreeNode> ls = new LinkedList<>();
ls.addLast(root);
while(!ls.isEmpty()) {
int size = ls.size();
for(int i = 0;i < size;i ++) {
TreeNode temp = ls.pollFirst();
res.add(temp.val);
if(temp.left != null) {
ls.addLast(temp.left);
}
if(temp.right != null) {
ls.addLast(temp.right);
}
}
}
return res;
}