面试题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. SQL总结(二)连表查询

    ---恢复内容开始--- SQL总结(二)连表查询 连接查询包括合并.内连接.外连接和交叉连接,如果涉及多表查询,了解这些连接的特点很重要. 只有真正了解它们之间的区别,才能正确使用. 1.Union ...

  2. each循环

    var NA_COUNT=0; var NG_OK_COUNT=0; //获取所有检验明细为同一个编号的下拉选项,看有没有不是N/A的下拉选项 $("#@(Perfix)tbData sel ...

  3. Sublime Text 3 插件安装及Vim 模式设置

    1.安装Sublime Text 3  下载安装:http://www.sublimetext.com/3 Package Control安装:https://sublime.wbond.net/in ...

  4. 使用lipo 查看静态库命令

    lipo -info .a 的: find . -name *.a -exec lipo -info "{}" \; framework 的: find . -name *.fra ...

  5. b/s开发者的困境

    作为一个由内而外都贴着web开发者标签的开发人员,注定会被大多数c/s开发者和通讯接口开发者所鄙视. 在他们看来,从事web开发,肯定很少用到算法,设计模式,多线程等等.我只能说,就我个人的经历来说, ...

  6. Xcode取消某条警告

      [Xcode取消某条警告] 像下面这样,把双引号“”内的内容替成实际的警告类型即可. #pragma clang diagnostic push #pragma clang diagnostic ...

  7. ServletContextListener使用详解

    在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...

  8. jface的CheckboxTreeViewer实现单选

    需求:使用FilteredTree实现一个下面这样的Dialog,要求Check框单选,即只能选择一个:当选择新的时候,旧的不选.说明:FilteredTree自带一个文本输入框. 1.自己的类继承o ...

  9. 8,SFDC 管理员篇 - 数据模型 - 公式和验证 2

    1, Checkbox 只接受真值或者假值 And(arg1, arg2....)至少两个参数,只有参数都为真时候,才返回真,只要有一个为假,就都为假 例如:AND(DoNotCall, HasOpt ...

  10. win10 Administrator没有管理员权限解决方案

    方法/步骤 在运行框 里面输入“gpedit.msc”. 打开组策略编辑器,计算机配置->windows设置->安全设置->->本地策略->安全选项 找到“用户账户控制: ...