[剑指offer]——Java题解(16~20)

目录

16 合并两个排序的链表

17 数的子结构

18 二叉树的镜像

19 顺时针打印矩阵

20 包含min函数的栈


16 合并两个排序的链表


题目:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

知识点: “链表”

方法1

分析:递归法,递归比较两链表对应节点的值,较小的节点先合并。

/*Java  runtime = 18ms*/
/*
public class ListNode {
    int val;
    ListNode next = null;
​
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null)
            return list2;
        if(list2 == null)
            return list1;
        ListNode tempNode = null;
        if(list1.val <= list2.val) {
            tempNode = list1;
            tempNode.next = Merge(list1.next,list2)
        }else {
            tempNode = list2;
            tempNode.next = Merge(list1,list2.next);
        }
        return tempNode;
    }
}

方法2

分析:非递归法,循环比较两链表对应节点的值,较小的节点先合并。

/*Java  runtime = 21ms*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null)
            return list2;
        if(list2 == null)
            return list1;
        ListNode listNode = new ListNode(0); 
        ListNode tempNode = listNode;
        while(list1 != null && list2 != null) {
            if(list1.val <= list2.val) {
                tempNode.next = list1;
                list1 = list1.next;
            }else {
                tempNode.next = list2;
                list2 = list2.next;
            }
            tempNode = tempNode.next;
       }
        if(list1 == null) {
            tempNode.next = list2;
        }
        else {
            tempNode.next = list1;
        }
        return listNode.next;
    }
}

17 数的子结构


题目:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)。

知识点: “树”

分析:递归法,若根节点相等,利用递归比较他们的子树是否相等,若根节点不相等,则利用递归分别在左右子树中查找。

/*Java  runtime = 16ms*/
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        boolean result = false;
        if (root2 != null && root1 != null) {
            if(root1.val == root2.val){
                result = IsSubtree(root1,root2);
            }
            if (!result)  //表示root1和root2相等但没有子树以及root1和root2不等两种情况
                return HasSubtree(root1.left,root2) || HasSubtree(root1.right,root2);
        }
        return result;
    }
    public boolean IsSubtree(TreeNode node1, TreeNode node2) {
        if (node2 == null) {  //node2遍历完了,说明都能对应上,返回true
            return true;
        }
        if (node1 == null) {   //node2非空时node1为空,说明子树过长,返回false
            return false;
        }
        if (node1.val != node2.val) {
            return false;
        }
        return IsSubtree(node1.left,node2.left) && 
                    IsSubtree(node1.right,node2.right);
    }
}

18 二叉树的镜像


题目:操作给定的二叉树,将其变换为源二叉树的镜像。

知识点: “树”

方法1

分析:递归法,使用递归方式交换每个节点的左右子树位置。

/*Java  runtime = 21ms*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null)
            return;
        if(root.left == null && root.right == null)
            return;
        //递归交换叶子节点
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if(root.left != null)
            Mirror(root.left);
        if(root.right != null)
            Mirror(root.right);
    }
}

方法2

分析:非递归法,使用递归方式交换每个节点的左右子树位置。

/*Java  runtime = 20ms*/
import java.util.Stack;
public class Solution {
    public void Mirror(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                TreeNode temp = root.left;
                root.left = root.right;
                root.right = temp;
                stack.push(root);
                root = root.left;
            }
            if (!stack.isEmpty()) {
                root = stack.pop();
                root = root.right;
            }
        }
    }
}

19 顺时针打印矩阵


题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

知识点: “数组”

分析:先得到矩阵的行和列数,然后依次旋转打印数据,一次旋转打印结束后,往对角分别前进和后退一个单位。用一个变量count记录打印次数,如果count等于总的元素数,就跳出循环。

/*Java  runtime = 26ms*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
       ArrayList<Integer> resultList = new ArrayList<>();
       int rows = matrix.length;
       int cols = matrix[0].length;
       if(rows == 0 && cols ==0)
           return resultList;
       int left=0,top=0,bottom=rows-1,right=cols-1;
       int count = 0;//计数,count如果达到数组的全部个数,那么结束。
       while(count < cols*rows)
       {
           for(int i=left;i<=right;i++)//从左往右进行遍历,第一层
           {//left是目前最左边的那个边界,right是目前最右边的边界
               resultList.add(matrix[top][i]);
               count++;
               if(count == cols*rows)
                   return resultList;
           }
           top++;//遍历完目前的最顶层,那么top就到下一层
           for(int i=top;i<=bottom;i++)
           {//从上往下进行遍历,top是目前最上的边界,bottom是目前最下的边界
               resultList.add(matrix[i][right]);
               count++;
               if(count == cols*rows)
                   return resultList;
           }
           right--;//遍历完最右边的边界,那么right就减一,到下一个最右边边界
           for(int i=right;i>=left;i--)
           {//从右到左,和上面同理
               resultList.add(matrix[bottom][i]);
               count++;
               if(count == cols*rows)
                   return resultList;
           }
           bottom--;
           for(int i=bottom;i>=top;i--)
           {//从下到上,和上面同理。
               resultList.add(matrix[i][left]);
               count++;
               if(count == cols*rows)
                   return resultList;
           }
           left++;
       }
       return resultList;
    }
}

20 包含min函数的栈


题目:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

知识点: “栈”

分析:定义两个栈,stack1存放入的值,stack2存最小值。

/*Java  runtime = 21ms*/
import java.util.Stack;
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    public void push(int node) {
        stack1.push(node);
        if (stack2.isEmpty()) {  // stack2.empty()也可以
            stack2.push(node);
        }else {
        //当元素小于stack2的栈顶元素时才能入栈,保证其栈顶元素始终是栈内元素的最小值
            if (stack2.peek() > node) {    
                stack2.push(node);
            }
        }
    }
​
    public void pop() {
    //如果stack2的栈顶元素和stack1的栈顶元素相等,同时出栈;不等则只有stack1出栈
        if (stack1.pop() == stack2.peek()) {  
        //Stack类里的pop()方法返回的是对象,这里定义的函数的是无返回值的
            stack2.pop();
        }
    }
​
    public int top() {
        return stack1.peek();
    }
​
    public int min() {
        return stack2.peek();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值