随笔分类 -  剑指offer

摘要:题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5使用递归 1 public void Mirror(TreeNode root) 阅读全文
posted @ 2019-04-11 16:31 月半榨菜 阅读(99) 评论(0) 推荐(0)
摘要:题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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. 首先判断需要走几圈 阅读全文
posted @ 2019-04-11 16:25 月半榨菜 阅读(100) 评论(0) 推荐(0)
摘要:题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 根据前序遍历序列找到子树根节点,并在中序遍历序列中找 阅读全文
posted @ 2019-04-10 21:59 月半榨菜 阅读(123) 评论(0) 推荐(0)
摘要:题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。 中序遍历二叉搜索树,找到第k个结点,除了使用递归,还可以使用栈实现非递归写法 1 public class Solution {//树 my 2 int ind 阅读全文
posted @ 2019-04-10 21:53 月半榨菜 阅读(107) 评论(0) 推荐(0)
摘要:题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 使用双向链表,先把当前行按顺序打印,再取出当前行并将下一行存入链表 1 public ArrayList<ArrayList<Integer> 阅读全文
posted @ 2019-04-09 21:29 月半榨菜 阅读(106) 评论(0) 推荐(0)
摘要:题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 若结点存在右孩子,则右孩子的最坐下结点为中序遍历下一个结点 若没有右孩子,则该结点为祖先的左子树最右下结点的最近的一个祖先,为下一个结点 public T 阅读全文
posted @ 2019-04-09 19:51 月半榨菜 阅读(84) 评论(0) 推荐(0)
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmet 阅读全文
posted @ 2019-04-09 19:45 月半榨菜 阅读(85) 评论(0) 推荐(0)
摘要:题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 使用dp,当n时,选着竖着放一个,那么后面的可能性为f(n-1) 选择横着放一个,那么要占两个位置,后面的可能性为f(n-2) 故f(n)=f(n-1)+f(n 阅读全文
posted @ 2019-04-09 16:18 月半榨菜 阅读(63) 评论(0) 推荐(0)
摘要:题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。 f(n)=f(n-1)+f(n-2)+...+f(n-n) =f(0)+f(1)+...+f(n-1) f(n-1)=f(n-2)+f(n-3)+...+f(n-n)=f(0)+ 阅读全文
posted @ 2019-04-09 15:51 月半榨菜 阅读(94) 评论(0) 推荐(0)
摘要:题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。 方法一:利用ArrayList库函数 1 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {//链表 my 2 ArrayList<I 阅读全文
posted @ 2019-04-09 11:45 月半榨菜 阅读(95) 评论(0) 推荐(0)
摘要:题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5 1 public ListNode deleteDuplication(ListNode pHead) 2 {//链 阅读全文
posted @ 2019-04-09 11:27 月半榨菜 阅读(124) 评论(0) 推荐(0)
摘要:题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。 方法一:逐个相乘,时间复杂度为O(n2) 1 public int[] multipl 阅读全文
posted @ 2019-04-08 20:31 月半榨菜 阅读(99) 评论(0) 推荐(0)
摘要:题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 方法一:从[0][0]逐个查找,时间复杂度为O(n2),空间复杂度为O(1) 方法二:从[0][ 阅读全文
posted @ 2019-04-08 19:44 月半榨菜 阅读(65) 评论(0) 推荐(0)
摘要:题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。 方法一:使用数组记录数 阅读全文
posted @ 2019-04-08 19:33 月半榨菜 阅读(85) 评论(0) 推荐(0)
摘要:Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjac 阅读全文
posted @ 2019-04-02 11:13 月半榨菜 阅读(119) 评论(0) 推荐(0)
摘要:Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). Example 1: Example 2: Exa 阅读全文
posted @ 2019-03-31 16:38 月半榨菜 阅读(139) 评论(0) 推荐(0)
摘要:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3 阅读全文
posted @ 2019-03-26 10:37 月半榨菜 阅读(81) 评论(0) 推荐(0)
摘要:Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Example 2: Example 3: Note: -100.0 < x < 100.0 n is a 32-bit signed int 阅读全文
posted @ 2019-03-25 15:46 月半榨菜 阅读(119) 评论(0) 推荐(0)
摘要:Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k num 阅读全文
posted @ 2019-03-20 21:18 月半榨菜 阅读(135) 评论(0) 推荐(0)
摘要:Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front 阅读全文
posted @ 2019-03-20 15:20 月半榨菜 阅读(134) 评论(0) 推荐(0)