- 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 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) {
int row = matrix.length;
int col = matrix[0].length;
ArrayList<Integer> res = new ArrayList<>();
if(row == 0 && col == 0)
return res;
int left = 0, right = col - 1, top = 0, bottom = row - 1;
while(left <= right && top <= bottom){
//上:从左到右
for(int i=left; i<=right; i++)
res.add(matrix[top][i]);
//右:从上到下
for(int i=top+1; i<=bottom; i++)
res.add(matrix[i][right]);
//下:从右到左
if(top != bottom){
//防止单行情况
for(int i=right-1; i>=left; i--)
res.add(matrix[bottom][i]);
}
//左:从下到上
if(left != right){
//防止单列情况
for(int i=bottom-1; i>top; i--)
res.add(matrix[i][left]);
}
left++; right--; top++; bottom--;
}
return res;
}
}
- 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
解题思路
用一个栈stack保存数据,用另外一个栈temp保存依次入栈最小的数
比如,stack中依次入栈
5, 3, 4, 10, 2, 12, 1, 8
则temp依次入栈
5, 3, 3,3, 2, 2, 1, 1
每次入栈的时候,如果入栈的元素比min中的栈顶元素小或等于则入栈,否则用最小元素入栈。
public class Solution {
Stack<Integer> stack = new Stack<>();
Stack<Integer> temp = new Stack<>();
int min = Integer.MAX_VALUE;
public void push(int node) {
stack.push(node);
if(node < min){
temp.push(node);
min = node;
}
else
temp.push(min);
}
public void pop() {
stack.pop();
temp.pop();
}
public int top() {
int t = stack.pop();
stack.push(t);
return t;
}
public int min() {
int m = temp.pop();
temp.push(m);
return m;
}
}
- 题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
解题思路
模拟堆栈操作的过程,将原数列依次压栈,把栈顶元素与所给出栈队列相比,如果相同则出栈,如果不同则继续压栈,直到原数列中所有数字压栈完毕。最后,检测栈中是否为空,若空,说明出栈队列可由原数列进行栈操作得到。否则,说明出栈队列不能由原数列进行栈操作得到。
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length != popA.length ||
pushA.length == 0 ||
popA.length == 0)
return false;
Stack<Integer> stack = new Stack<>();
int index = 0;
for(int i = 0; i < pushA.length; i++){
stack.push(pushA[i]);
while(!stack.empty() && stack.peek() == popA[index]){
stack.pop();
index++;
}
}
return stack.empty();
}
}
- 题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
解题思路
就是二叉树的层序遍历。借助一个队列就可以实现。
使用两个队列一个存放节点,一个存放值。先将根节点加入到队列中,然后遍历队列中的元素,遍历过程中,访问该元素的左右节点,再将左右子节点加入到队列中来。
注意Queue创建的方式:Queue queue = new LinkedList();
用add将元素添加到队列中,用remove来移除并返回队首元素。
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
if(root == null)
return res;
//Queue is abstract; 不能用 new Queue<TreeNode>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(queue.size() != 0){
root = queue.remove();
res.add(root.val);
if(root.left != null){
queue.add(root.left);
}
if(root.right != null){
queue.add(root.right);
}
}
return res;
}
}
- 题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
结题思路:
对于后序遍历来说,序列数组的最后一个元素一定是根节点, 根据这个元素,将前面的数组分为左、右两个部分,左侧部分都比该元素小,右侧部分都比该元素大,如果右侧部分有比该根节点小的元素,那么就不是后序遍历,如此递归进行。
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length == 0)
return false;
if(sequence.length == 1)
return true;
return judge(sequence, 0, sequence.length-1);
}
public boolean judge(int [] sequence, int start, int root){
if(start >= root)
return true;
int i = start;
while(i < root && sequence[i] < sequence[root])
i++;
for(int j=i; j<root; j++){
if(sequence[j]<sequence[root])
return false;
}
return (judge(sequence, start, i-1)) && (judge(sequence, i, root-1));
}
}
- 题目描述
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
解题思路
用前序遍历的方式访问到某一结点时,把该结点添加到路径上,并用目标值减去该节点的值。如果该结点为叶结点并且目标值减去该节点的值刚好为0,则当前的路径符合要求,我们把加入res数组中。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到它的父结点。因此我们在函数退出之前要在路径上删除当前结点,以确保返回父结点时路径刚好是从根结点到父结点的路径。
public class Solution {
ArrayList<ArrayList<Integer>> pathList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path = new ArrayList<Integer>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root == null)
return pathList;
path.add(root.val);
if(root.left == null && root.right == null && target == root.val)
{
pathList.add(new ArrayList<Integer>(path));
}
if(root.val <= target && root.left != null){
FindPath(root.left,target-root.val);
}
if(root.val <= target && root.right != null)
{
FindPath(root.right,target-root.val);
}
path.remove(path.size()-1);//回退到父节点
return pathList;
}
}
- 题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
解题思路
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
if(pHead == null)
return null;
//复制节点 A->B->C 变成 A->A'->B->B'->C->C'
RandomListNode head = pHead;
while(head != null){
RandomListNode node = new RandomListNode(head.label);
node.next = head.next;
head.next = node;
head = node.next;
}
//复制random
head = pHead;
while(head != null){
head.next.random = head.random == null ? null : head.random.next;
head = head.next.next;
}
//折分
head = pHead;
RandomListNode chead = head.next;
while(head != null){
RandomListNode node = head.next;
head.next = node.next;
node.next = node.next == null ? null : node.next.next;
head = head.next;
}
return chead;
}
}
- 题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
解题思路:
在二叉搜索树中,左子结点的值总是小于父结点的值,右子节点的值总是大于父结点的值。因此我们在转换成排序双向链表时,原先指向左子结点的指针调整为链表中指向前一个结点的指针,原先指向右子节点的指针调整为链表中指向后一个结点的指针。
因为中序遍历是按照从小到大的顺序遍历二叉搜索树,所以我们用中序遍历树中的每一个节点得到的正好是要求的排好序的。遍历过程如下:
每次遍历节点的左孩子、右孩子,把左孩子指向转换链表的尾节点,并把末尾指针的右孩子指向自己。右孩子指向节点的右孩子。如果没有右孩子就返回。这一过程可以用递归实现。
//1、将左子树构成双链表,并返回该链表的头节点(左子树最左边的节点)
//2、定位到左链表的最后一个节点(左子树最右边的节点)
//3、如果左子树链表不为空,则将当前root追加到左子树链表后
//4、将右子树构造成双向链表,并返回链表头结点(右子树最左边的节点)
//5、如果右子树链表不为空,将右子树链表追加到当前root后
//6,根据左子树链表是否为空返回的整体双向链表的头节点
//Convert函数把一个二叉搜索树变成一个有序的双向链表,返回双向链表的头结点,参数root为二叉搜索树的根节点
public TreeNode Convert(TreeNode root){
if(root==null){//假如根节点为空,返回空
return null;
}
if(root.left==null&&root.right==null){//假如只有一个根节点,则返回根节点
return root;
}
//1、将左子树构造成双链表,并返回该链表头结点left
TreeNode left=Convert(root.left);
//2、定位到左子树链表的最后一个节点(左子树最右边的节点)
TreeNode p=left;//创建一个临时节点P,用来遍历找到左链表的最后一个节点(左子树最右边的节点),p初始化指向做左子树的根节点,
while(p!=null&&p.right!=null){
p=p.right;//最终p为左子树最右边的节点
}
//3、如果左子树链表不为空,将当前root追加到左子树链表后
if(left!=null){//左子树链表不为空
p.right=root;//左子树链表的最后一个节点p(左子树最右边节点)的右指针指向当前root节点
root.left=p;//当前root节点的左指针指向左子树链表的最后一个节点p(左子树最右边节点)
}
//4、将右子树构造成双链表,并返回该链表的头结点right
TreeNode right=Convert(root.right);
//5、如果右子树链表不为空,将右子树链表追加到当前root后
if(right!=null){//右子树链表不为空
right.left=root;//右子树链表的头结点right的左指针指向当前root
root.right=right;//当前root的右指针指向右子树链表的头结点right
}
return left!=null?left:root;//根据左子树链表是否为空返回整个双向链表的头指针。
}