剑指offer java实现合集(4)第16~20题

16.合并两个排序的链表

依次比较两个链表当前节点的值,小的就插入到后年,如果有任何一条链表走到了结尾,就直接加载另外一条链表剩余的节点。

注意要提前保存头结点。

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode first = new ListNode(-1);
        ListNode cur = first;
        while(list1!=null&&list2!=null){
            if(list1.val<list2.val){
                first.next = list1;
                first = first.next;
                list1 = list1.next;
            }else{
                first.next = list2;
                first = first.next;
                list2 = list2.next;
            }
        }
        while(list1!=null){
            first.next = list1;
            first = first.next;
            list1 = list1.next;
        }
         while(list2!=null){
            first.next = list2;
            first = first.next;
            list2 = list2.next;
        }
        return cur.next;
    }
}

17.树的子结构

写一个递归比较的方法,每次找到与B树根节点的值相同的位置,就调用该方法进行判断。

public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root2==null){
            return false;
        }
        if(root1==null&&root2!=null){
            return false;
        }
        boolean flag = false;
        if(root1.val==root2.val){
            flag = SameTree(root1,root2);
        }
        if(!flag){
            flag = HasSubtree(root1.left,root2);
        }
        if(!flag){
            flag = HasSubtree(root1.right,root2);
        }
        return flag;
    }
    public boolean SameTree(TreeNode root1,TreeNode root2){
        if(root2==null){
            return true;
        }
        if(root1==null){
            return false;
        }
        if(root1.val ==root2.val){
            return SameTree(root1.left,root2.left)&&SameTree(root1.right,root2.right);
        }else{
            return false;
        }
    }
}

18.二叉树的镜像

类似于两个数交换顺序,只不过这次是树的左右节点。

public class Solution {
    public void Mirror(TreeNode root) {
        if(root==null){
          return ;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        Mirror(root.left);
        Mirror(root.right);
    }
}

19.顺时针打印数组

从最外圈向里打印,选择左上角和右下角作为参考,注意每一个拐点的要求。

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> res = new ArrayList();
        if(matrix==null||matrix.length==0){
            return res;
        }
        int srow = 0;
        int sclu = 0;
        int erow = matrix.length-1;
        int eclu = matrix[0].length-1;
        while(srow<=erow&&sclu<=eclu){
            getres(matrix,res,srow++,sclu++,erow--,eclu--);
        }
        return res;
    }
    public void getres(int [][]num,ArrayList<Integer> res,int srow,int sclu,int erow,int eclu){
        if(srow==erow){
            for(int i =sclu;i<=eclu;i++){
                res.add(num[srow][i]);
            }
        }else if(sclu==eclu){
            for(int i =srow;i<=erow;i++){
                res.add(num[i][sclu]);
            }
        }else{
            int tclu = sclu;
            int trow = srow;
            while(tclu!=eclu){
                res.add(num[srow][tclu]);
                tclu++;
            }
            while(trow!=erow){
                res.add(num[trow][eclu]);
                trow++;
            }
            while(tclu!=sclu){
                res.add(num[erow][tclu]);
                tclu--;
            }
            while(trow!=srow){
                res.add(num[trow][sclu]);
                trow--;
            }
        }
    }
}

20.包含min函数的栈

import java.util.Stack;

public class Solution {

     Stack<Integer> st1 = new Stack();
    Stack<Integer> st2 = new Stack();
    public void push(int node) {
        if(st1.isEmpty()){
            st1.push(node);
            st2.push(node);
        }else{
            st1.push(node);
            if(node<st2.peek()){
                st2.push(node);
            }
        }
        
    }
    
    public void pop() {
        if(st1.peek()==st2.peek()){
            st1.pop();
            st2.pop();
        }else{
            st1.pop();
        }
    }
    
    public int top() {
        return st1.peek();
    }
    
    public int min() {
        return st2.peek();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40106836/article/details/89387138
今日推荐