16、合并两个排序的链表
题目描述:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
解题思路:先确定哪一个表头最小,以这个最小的链表为标准,一一进行比较,注意list1和list2为空的情况
/*
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 min;
ListNode max;
if(list1.val <= list2.val) {
min = list1;
max = list2;
}else {
min = list2;
max = list1;
}
ListNode head = min;
boolean isOver1 = false;
boolean isOver2 = false;
while(min.next != null) {
int num1 = min.val;
int num2 = max.val;
int num3 = min.next.val;
if(num1 <= num2 && num2 <= num3) {
ListNode temp = min.next;
ListNode newNode = new ListNode(num2);
newNode.next = temp;
min.next = newNode;
if(max.next == null) {
isOver2 = true;
break;
}else {
max = max.next;
min = min.next;
}
}else if(num2 > num3) {
min = min.next;
}
}
if( !isOver2 && max != null && min.val < max.val) {
min.next = max;
}
return head;
}
}
17、树的子结构
题目描述:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
解题思路:先判断A、B两棵树的根节点是否相等,如果相等,则递归比较他们的子树是否相等。如果根节点不相等,则递归在左右子树查找与B根节点相等的节点,以此节点为根节点再次进行比较(注意当根节点相同,比较子树时,先判断B这边是否为空,再判断A那边是否为空)
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1 == null || root2 == null) {
return false;
}
boolean result = false;
if(root1.val == root2.val) {
result = doesTree1HasTree2(root1, root2);
}
if(!result) {
result = HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
}
return result;
}
public boolean doesTree1HasTree2(TreeNode root1, TreeNode root2) {
if(root2 == null) {
return true;
}
if(root1 == null) {
return false;
}
if(root1.val != root2.val) {
return false;
}
return doesTree1HasTree2(root1.left, root2.left) && doesTree1HasTree2(root1.right, root2.right);
}
}
18、二叉树的镜像
题目描述:操作给定的二叉树,将其变换为源二叉树的镜像。
解题思路:用递归镜像二叉树的子树
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public void Mirror(TreeNode root) {
/*root = getNode(root)就可以了,但是这样写牛客网就是过不了╭(╯^╰)╮*/
TreeNode newRoot = getNode(root);
if(newRoot == null) {
root = null;
}else {
root.val = newRoot.val;
root.left = newRoot.left;
root.right = newRoot.right;
}
}
public TreeNode getNode(TreeNode root) {
if(root == null) {
return null;
}
TreeNode newRoot = new TreeNode(root.val);
newRoot.left = getNode(root.right);
newRoot.right = getNode(root.left);
return newRoot;
}
}
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.
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> list = new ArrayList<>();
int row = matrix.length;
int line = matrix[0].length;
int i = 0;
int j = 0;
int ru = 0;
int rd = 0;
int ll = 0;
int lr = 0;
while(list.size() < row * line) {
while (list.size() < row * line && j < line - lr) {
list.add(matrix[i][j]);
j++;
}
ru++; //走过一行
j--;
i++;
while (list.size() < row * line && i < row - rd) {
list.add(matrix[i][j]);
i++;
}
lr++; //走过一列
i--;
j--;
while(list.size() < row * line && j >= ll) {
list.add(matrix[i][j]);
j--;
}
rd++;
j++;
i--;
while(list.size() < row * line && i >= ru) {
list.add(matrix[i][j]);
i--;
}
ll++;
i++;
j++;
}
return list;
}
}
20、包含min函数的栈
题目描述:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
import java.util.Stack;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
private Stack<Integer> stack = new Stack<>();
public void push(int node) {
stack.push(node);
}
public void pop() {
stack.pop();
}
public int top() {
int result = stack.pop();
stack.add(result);
return result;
}
public int min() {
ArrayList<Integer> copy = new ArrayList<>();
while(!stack.empty()){
copy.add(stack.pop());
}
for(int i=copy.size()-1;i>=0;i--){
stack.push(copy.get(i));
}
Collections.sort(copy);
return copy.get(0);
}
}