剑指offer题目61-67
面试题61:把二叉树打印成多行
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
travel(pRoot,res,0);
return res;
}
public void travel(TreeNode cur,ArrayList<ArrayList<Integer>> res,int level){
if(cur==null) return;
if(res.size()<=level){
ArrayList<Integer> newLevel = new ArrayList<Integer>();
res.add(newLevel);
}
ArrayList<Integer> col = res.get(level);
col.add(cur.val);
travel(cur.left,res,level+1);
travel(cur.right,res,level+1);
}
}
面试题62:序列化二叉树
import java.util.Queue;
import java.util.LinkedList;
public class Solution {
String Serialize(TreeNode root) {
if(root == null) return "#!";
String res = root.val + "!";
res += Serialize(root.left);
res += Serialize(root.right);
return res;
}
TreeNode Deserialize(String str) {
String[] values = str.split("!");
Queue<String> queue = new LinkedList<String>();
for(int i=0;i<values.length;i++){
queue.offer(values[i]);
}
return recover(queue);
}
TreeNode recover(Queue<String> queue){
String value = queue.poll();
if(value.equals("#")){
return null;
}
TreeNode root = new TreeNode(Integer.valueOf(value));
root.left = recover(queue);
root.right = recover(queue);
return root;
}
}
面试题63:二叉搜索树的第K个节点
import java.util.ArrayList;
public class Solution {
public ArrayList<TreeNode> res = new ArrayList<TreeNode>();
TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot==null || k==0) return null;
inorderTra(pRoot);
if(k<=0 || k>res.size()) return null;
return res.get(k-1);
}
public void inorderTra(TreeNode root){
if(root == null) return ;
inorderTra(root.left);
res.add(root);
inorderTra(root.right);
}
}
补充第二种做法:
public class Solution {
public TreeNode res ;
public int cnt = 0;
TreeNode KthNode(TreeNode pRoot, int k)
{
cnt = k;
if(pRoot == null) return pRoot;
help(pRoot);
return res;
}
public void help(TreeNode p){
if(p.left!=null){
help(p.left);
}
cnt--;
if(cnt == 0){
res = p;
return;
}
if(p.right!=null){
help(p.right);
}
return;
}
}
面试题64:数据流中的中位数
import java.util.PriorityQueue;
import java.util.Collections;
public class Solution {
private PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
private PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(5000,Collections.reverseOrder());
public void Insert(Integer num) {
if(maxHeap.isEmpty() || num<maxHeap.peek()){
maxHeap.offer(num);
}else{
minHeap.offer(num);
}
if(maxHeap.size() > minHeap.size() + 1){
minHeap.offer(maxHeap.poll());
}else if(minHeap.size() > maxHeap.size()){
maxHeap.offer(minHeap.poll());
}
}
public Double GetMedian() {
if(maxHeap.size() > minHeap.size()){
return (double)maxHeap.peek();
}else{
return (maxHeap.peek() + minHeap.peek())/2.0;
}
}
}
面试题65:滑动窗口的最大值
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> maxInWindows(int [] num, int size)
{
ArrayList<Integer> res = new ArrayList<Integer>();
if(num == null || size<=0){
return res;
}
int n = num.length;
Deque<Integer> queue = new ArrayDeque<Integer>();
for(int i=0;i<num.length;i++){
while(!queue.isEmpty() && queue.peek()<i-size+1){
queue.poll();
}
while(!queue.isEmpty() && num[queue.peekLast()]<num[i]){
queue.pollLast();
}
queue.offer(i);
if(i>=size-1){
res.add(num[queue.peek()]);
}
}
return res;
}
}
面试题66:矩阵中的路径
为什么这么写就不对?
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
{
if(matrix==null||rows<1||cols<1||str==null) return false;
boolean[] visited = new boolean[matrix.length];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(dfs(matrix,rows,cols,i,j,0,str,visited)){
return true;
}
}
}
return false;
}
public boolean dfs(char[] matrix,int rows,int cols,int i,int j,int k,char[] str,boolean[] visited){
if(k == str.length - 1){
return true;
}
int index = i*cols+j;
boolean hasPath = false;
if(i>=0&&i<rows&&j>=0&&j<cols&&matrix[index]==str[k]&&visited[index]==false){
visited[index] = true;
hasPath = dfs(matrix,rows,cols,i,j+1,k+1,str,visited)
||dfs(matrix,rows,cols,i,j-1,k+1,str,visited)
||dfs(matrix,rows,cols,i+1,j,k+1,str,visited)
||dfs(matrix,rows,cols,i-1,j,k+1,str,visited);
visited[index] = false;
}
return hasPath;
}
}
这样写就可以
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
int flag[] = new int[matrix.length];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (helper(matrix, rows, cols, i, j, str, 0, flag))
return true;
}
}
return false;
}
private boolean helper(char[] matrix, int rows, int cols, int i, int j, char[] str, int k, int[] flag) {
int index = i * cols + j;
if (i < 0 || i >= rows || j < 0 || j >= cols || matrix[index] != str[k] || flag[index] == 1)
return false;
if(k == str.length - 1) return true;
flag[index] = 1;
if (helper(matrix, rows, cols, i - 1, j, str, k + 1, flag)
|| helper(matrix, rows, cols, i + 1, j, str, k + 1, flag)
|| helper(matrix, rows, cols, i, j - 1, str, k + 1, flag)
|| helper(matrix, rows, cols, i, j + 1, str, k + 1, flag)) {
return true;
}
flag[index] = 0;
return false;
}
}
面试题67:机器人的运动范围
public class Solution {
public int movingCount(int threshold, int rows, int cols)
{
boolean[] visited = new boolean[rows*cols];
int res = count(0,0,rows,cols,threshold,visited);
return res;
}
public int count (int i,int j,int rows,int cols,int threshold,boolean[] visited){
int cnt = 0;
int index = i*cols+j;
if(check(threshold,i,j,rows,cols,visited) == true){
visited[index] = true;
cnt = 1 + count(i+1,j,rows,cols,threshold,visited)
+ count(i-1,j,rows,cols,threshold,visited)
+ count(i,j+1,rows,cols,threshold,visited)
+ count(i,j-1,rows,cols,threshold,visited);
}
return cnt;
}
public boolean check(int threshold,int i,int j,int rows,int cols,boolean[] visited){
int valRow = getVal(i);
int valCol = getVal(j);
if(i>=0&&i<rows&&j>=0&&j<cols&&valRow+valCol<=threshold&&visited[i*cols+j]==false){
return true;
}
return false;
}
public int getVal(int i){
int val = 0;
while(i>0){
val += i%10;
i /= 10;
}
return val;
}
}
剑指offer题目61-67的更多相关文章
- 【剑指Offer】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指offer题目java实现
Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 剑指offer题目解答合集(C++版)
数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- win7下IIS错误:"无法访问请求的页面,因为该页的相关配置数据无效"的解决方法(转)
今天新装win7,然后在IIS下布署了一个网站,布署完成后运行,提示如下错误:HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效 ...
- Eclipse tomcat插件禁用热加载
Eclipse中的tomcat插件默认是开户了热加载,只要是修改了java文件一保存,tomcat自动编译.加载.发布,很吃内存. 关闭方法: 打开eclipse,找到server项: 双击打开,修改 ...
- SQL Server 2008 数据库通过镜像同步备份(数据库热备)
数据库镜像运行模式: 异步数据库镜像模式(异步,高性能模式) 同步数据库镜像模式(同步,高安全模式) 参考资料: http://technet.microsoft.com/zh-cn/library/ ...
- Windows下配置nginx根目录的问题
location / { root E:/xampp/htdocs/html5/php/yii2-rest-master/rest/web; index index.html index.htm; } ...
- OPTIMIZE TABLE的作用--转载
当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删 除操作后在数据文件中留下碎片所致.Discuz! 在系统数设置界面提供了数据表优化的功能,可以去除删除操作后留下的数据文件 ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- BMP图片格式
BMP图片 BMP采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BMP文件所占用的空间很大.BMP文件的图像深度可选lbit.4bit.8bit及24bit和32bit.BMP文 ...
- 翻译: TypeScript 1.8 Beta 发布
原文地址:https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/ 今天,我们发布了 ...
- 关于ILDASM.EXE的知识整理
因为现在用的VS2010,发现,这个工具自己就带着ILDASM.EXE这个反编译工具 具体的查找方式为: C:\Program Files\Microsoft SDKS\Windows\V7.0\bi ...
- lua 高级
io操作: io.input(filename):指定一个输入流,可以是标准输入stdin,也可以是一个文件路径,返回一个文件句柄: io.output(filename):指定一个输出流,可以是标准 ...