作者:手机用户2602896463 | 来源:互联网 | 2023-06-03 07:45
19.顺时针打印矩阵题目描述:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4X4矩阵:12345678910111213141516则依次打印出
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.
- 解题思路:计算好层数然后左,下,右,上这样循环输出注意判断是否重复输出即可。
public class printMatrix {
public ArrayList printMatrix(int [][] matrix) {
ArrayList result = new ArrayList<>();
int row = matrix.length;
int column = matrix[0].length;
if(row == 0 || column == 0){
return result;
}
int layer = (Math.min(row,column)-1)/2 + 1 ;
for (int i = 0;i for(int j = i ; j for(int j = i + 1; j for(int j = column - i - 2; (j > i - 1) &&(row-i-1!=i) ; j--){result.add(matrix[row - i - 1 ][j]);}
for(int j = row - i - 2; (j > i)&&(column-i-1!=i) ; j--){result.add(matrix[j][i]);}
}
return result;
}
}
20.包含min函数的栈
- 题目描述:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
- 解题思路:使用辅助栈每次存小的数。
Stack data = new Stack();
Stack min = new Stack();
Integer temp = null;
public void push(int node) {
if(temp != null){
if(node <= temp ){
temp = node;
min.push(node);
}
data.push(node);
}else{
temp = node;
data.push(node);
min.push(node);
}
}
public void pop() {
int num = data.pop();
int num2 = min.pop();
if(num != num2){
min.push(num2);
}
}
public int top() {
int num = data.pop();
data.push(num);
return num;
}
public int min() {
int num = min.pop();
min.push(num);
return num;
}
21.栈的压入、弹出序列
- 题目描述:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
- 解题思路:模拟入栈出栈的过程,出错返回false。
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack stack = new Stack<>();
if(pushA.length != popA.length){
return false;
}
int j = 0;
for (int i = 0; i stack.push(pushA[i]);
if (pushA[i] == popA[j]){
stack.pop();
j++;
}
}
while (!stack.isEmpty()){
if(popA[j] == stack.pop()){
j++;
}
else return false;
}
return true;
}
22.从上往下打印二叉树
- 题目描述:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
- 解题思路:思路是用arraylist模拟一个队列来存储相应的TreeNode。
public class PrintFromTopToBottom {
public ArrayList PrintFromTopToBottom(TreeNode root) {
ArrayList list = new ArrayList<>();
ArrayList queue = new ArrayList<>();
if (root == null) {
return list;
}
queue.add(root);
while (queue.size() != 0) {
TreeNode temp = queue.remove(0);
if (temp.left != null){
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
list.add(temp.val);
}
return list;
}
}
23.二叉搜索树的后序遍历序列
- 题目描述:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 解题思路:BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两段(子树)都是合法的后序序列。完美的递归定义 : ) 。
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length==0)
return false;
if(sequence.length==1)
return true;
return ju(sequence, 0, sequence.length-1);
}
public boolean ju(int[] a,int star,int root){
if(star>=root)
return true;
int i = root;
//从后面开始找
while(i>star&&a[i-1]>a[root])
i--;//找到比根小的坐标
//从前面开始找 star到i-1应该比根小
for(int j = star;j if(a[j]>a[root])
return false;;
return ju(a,star,i-1)&&ju(a, i, root-1);
}
24.二叉树中和为某一值的路径
- 题目描述:输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
- 解题思路:
private ArrayList> listAll = new ArrayList>();
private ArrayList list = new ArrayList();
public ArrayList> FindPath(TreeNode root,int target) {
if(root == null) return listAll;
list.add(root.val);
target -= root.val;
if(target == 0 && root.left == null && root.right == null)
listAll.add(new ArrayList(list));
FindPath(root.left, target);
FindPath(root.right, target);
list.remove(list.size()-1);
return listAll;
}