剑指编程(4)

来源:互联网 发布:淘宝高颜值平价零食 编辑:程序博客网 时间:2024/05/18 00:51

一、
输入一个链表,反转链表后,输出链表的所有元素。

public class Solution {    public ListNode ReverseList(ListNode head) {        if(head == null) {            return null;        }        ListNode pre = null;        ListNode cur = head;        while(cur.next != null) {            ListNode next = cur.next;            cur.next = pre;            pre = cur;            cur = next;        }        cur.next = pre;        return cur;    }}

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

public class Solution {    //用归并排序的思想合并    public ListNode Merge(ListNode list1,ListNode list2) {        if(list2 == null) {            return list1;        }        if(list1 == null) {            return list2;        }        ListNode head = null;        if(list1.val < list2.val) {            head = list1;            list1 = list1.next;        }else {            head = list2;            list2 = list2.next;        }        ListNode cur = head;        while(list1 != null && list2 != null) {            if(list1.val < list2.val) {                cur.next = list1;                list1 = list1.next;            }else {                cur.next = list2;                list2 = list2.next;            }            cur = cur.next;        }        while(list1 != null) {            cur.next = list1;            cur = cur.next;            list1 = list1.next;        }        while(list2 != null) {            cur.next = list2;            cur = cur.next;            list2 = list2.next;        }        return head;    }}

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

public class Solution {    //判断一棵树是否是另一颗树的非根非叶结点    public boolean HasSubtree(TreeNode root1,TreeNode root2) {        if(root1 == null || root2 == null) {            return false;        }        return isSubtree(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);    }    //同时遍历A和B,判断B是否和A的子树完全相同    public boolean isSubtree(TreeNode root1, TreeNode root2) {        //如果B为空,说明B遍历完了,与A的子树完全相同        if(root2 == null) {            return true;        }        if(root1 == null) {            return false;        }        if(root1.val == root2.val) {            return isSubtree(root1.left, root2.left) && isSubtree(root1.right, root2.right);        }else {            return false;        }    }}

四、
/*
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
/*

public class Solution {    //递归地从根节点开始将左右节点互换    public void Mirror(TreeNode root) {        if(root == null) {            return;        }        TreeNode tmp = root.left;        root.left = root.right;        root.right = tmp;        Mirror(root.left);        Mirror(root.right);    }}

五、
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,
例如,如果输入如下矩阵: 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.

public class Solution {    public ArrayList<Integer> printMatrix(int [][] matrix) {        if(matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {            return null;        }        int row = matrix.length;        int col = matrix[0].length;        ArrayList<Integer> res = new ArrayList<>();        int left = 0, right = col-1, up = 0, down = row - 1;        while(left <= right && up <= down) {            for(int i = left; i <= right; i++) {//从左到右                res.add(matrix[up][i]);            }            up++;            for(int i = up; i <= down; i++) {//从上到下                res.add(matrix[i][right]);            }            right--;            for(int i = right; up - 1 != down && i >= left; i--) {//从右到左,同时防止单行重复                res.add(matrix[down][i]);            }            down--;            for(int i = down; right + 1 != left && i >= up; i--) {//从下到上,同时防止单列重复                res.add(matrix[i][left]);            }            left++;        }        return res;    }}
原创粉丝点击