面试题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的更多相关文章

  1. 【剑指Offer】剑指offer题目汇总

      本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...

  2. 代码题 — 剑指offer题目、总结

    剑指offer题目总结:  https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...

  3. 剑指offer题目系列三(链表相关题目)

    本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...

  4. 再来五道剑指offer题目

    再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...

  5. 剑指 Offer 题目汇总索引

    剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格              ...

  6. 牛客网上的剑指offer题目

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...

  7. 剑指offer题目java实现

    Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...

  8. 剑指offer题目系列二

    本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...

  9. 剑指offer题目系列一

    本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...

  10. 剑指offer题目解答合集(C++版)

    数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...

随机推荐

  1. arcgis server 10.2安装后,忘记Manager的用户名和密码

    arcgis server 10.2安装完毕后,需要创建站点,创建站点时,填写管理站点的用户名和密码.自己不小心,创建完毕后,给忘记了用户名和密码.求助Esri,解决方法如下: (1)找到arcgis ...

  2. 一加3,CM13蓝牙共享互联网 无效。

    一加3准备把4G网络共享给魅族PRO5. 但在一加3的蓝牙设置里怎么勾选都无用. 最后发现在要在PRO5上设置才行. 1.在蓝牙列表中,点击带圈的感叹号. 2.选择“互联网访问”. - --

  3. Git+Github代码管理控制

    关于Git的发展历程这里就不多介绍了,有兴趣的话可以查阅一下Git的相关历程. 如果你在做的项目或者自己写的一些项目由于需要用的分布式.或者团队开发.再或者你不善于对代码进行定期备份,但又希望自己的代 ...

  4. Android 触摸手势基础 官方文档概览2

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  5. MapReduce编程job概念原理

    在Hadoop中,每个MapReduce任务都被初始化为一个job,每个job又可分为两个阶段:map阶段和reduce阶段.这两个阶段分别用两个函数来表示.Map函数接收一个<key,valu ...

  6. nodejs初探(四)实现一个多人聊天室

    我们实现的思路是,当有一个人发送过来消息,我们就广播给其他客户端. var net = require('net'); var chatServer = net.createServer(), cli ...

  7. 通过group by和having去除重复

    $sql="SELECT peisonghao FROM ecs_order_info_ly GROUP BY peisonghao HAVING COUNT(*) >1"; ...

  8. JavaScript中Math--random()/floor()/round()/ceil()

    Math.random():返回0-1之间的任意数,不包括0和1: Math.floor(num):返回小于等于num的整数,相当于四舍五入的四舍,不五入:例子:Math.floor(1.0);Mat ...

  9. SpringBoot之springfox(Swagger) (ApiDoc接口文档)

    Springfox的前身是swagger-springmvc,是一个开源的API doc框架,可以将我们的Controller的方法以文档的形式展现,基于Swagger. 官网地址:http://sp ...

  10. Python基础篇【第1篇】: Python基础

    Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...