面试题61:把二叉树打印成多行

  1. public class Solution {
  2. public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
  3. ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
  4. travel(pRoot,res,0);
  5. return res;
  6. }
  7. public void travel(TreeNode cur,ArrayList<ArrayList<Integer>> res,int level){
  8. if(cur==null) return;
  9. if(res.size()<=level){
  10. ArrayList<Integer> newLevel = new ArrayList<Integer>();
  11. res.add(newLevel);
  12. }
  13. ArrayList<Integer> col = res.get(level);
  14. col.add(cur.val);
  15. travel(cur.left,res,level+1);
  16. travel(cur.right,res,level+1);
  17. }
  18. }

面试题62:序列化二叉树

  1. import java.util.Queue;
  2. import java.util.LinkedList;
  3. public class Solution {
  4. String Serialize(TreeNode root) {
  5. if(root == null) return "#!";
  6. String res = root.val + "!";
  7. res += Serialize(root.left);
  8. res += Serialize(root.right);
  9. return res;
  10. }
  11. TreeNode Deserialize(String str) {
  12. String[] values = str.split("!");
  13. Queue<String> queue = new LinkedList<String>();
  14. for(int i=0;i<values.length;i++){
  15. queue.offer(values[i]);
  16. }
  17. return recover(queue);
  18. }
  19. TreeNode recover(Queue<String> queue){
  20. String value = queue.poll();
  21. if(value.equals("#")){
  22. return null;
  23. }
  24. TreeNode root = new TreeNode(Integer.valueOf(value));
  25. root.left = recover(queue);
  26. root.right = recover(queue);
  27. return root;
  28. }
  29. }

面试题63:二叉搜索树的第K个节点

  1. import java.util.ArrayList;
  2. public class Solution {
  3. public ArrayList<TreeNode> res = new ArrayList<TreeNode>();
  4. TreeNode KthNode(TreeNode pRoot, int k)
  5. {
  6. if(pRoot==null || k==0) return null;
  7. inorderTra(pRoot);
  8. if(k<=0 || k>res.size()) return null;
  9. return res.get(k-1);
  10. }
  11. public void inorderTra(TreeNode root){
  12. if(root == null) return ;
  13. inorderTra(root.left);
  14. res.add(root);
  15. inorderTra(root.right);
  16. }
  17. }
  18. 补充第二种做法:
  19. public class Solution {
  20. public TreeNode res ;
  21. public int cnt = 0;
  22. TreeNode KthNode(TreeNode pRoot, int k)
  23. {
  24. cnt = k;
  25. if(pRoot == null) return pRoot;
  26. help(pRoot);
  27. return res;
  28. }
  29. public void help(TreeNode p){
  30. if(p.left!=null){
  31. help(p.left);
  32. }
  33. cnt--;
  34. if(cnt == 0){
  35. res = p;
  36. return;
  37. }
  38. if(p.right!=null){
  39. help(p.right);
  40. }
  41. return;
  42. }
  43. }

面试题64:数据流中的中位数

  1. import java.util.PriorityQueue;
  2. import java.util.Collections;
  3. public class Solution {
  4. private PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
  5. private PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(5000,Collections.reverseOrder());
  6. public void Insert(Integer num) {
  7. if(maxHeap.isEmpty() || num<maxHeap.peek()){
  8. maxHeap.offer(num);
  9. }else{
  10. minHeap.offer(num);
  11. }
  12. if(maxHeap.size() > minHeap.size() + 1){
  13. minHeap.offer(maxHeap.poll());
  14. }else if(minHeap.size() > maxHeap.size()){
  15. maxHeap.offer(minHeap.poll());
  16. }
  17. }
  18. public Double GetMedian() {
  19. if(maxHeap.size() > minHeap.size()){
  20. return (double)maxHeap.peek();
  21. }else{
  22. return (maxHeap.peek() + minHeap.peek())/2.0;
  23. }
  24. }
  25. }

面试题65:滑动窗口的最大值

  1. import java.util.Deque;
  2. import java.util.ArrayDeque;
  3. import java.util.ArrayList;
  4. public class Solution {
  5. public ArrayList<Integer> maxInWindows(int [] num, int size)
  6. {
  7. ArrayList<Integer> res = new ArrayList<Integer>();
  8. if(num == null || size<=0){
  9. return res;
  10. }
  11. int n = num.length;
  12. Deque<Integer> queue = new ArrayDeque<Integer>();
  13. for(int i=0;i<num.length;i++){
  14. while(!queue.isEmpty() && queue.peek()<i-size+1){
  15. queue.poll();
  16. }
  17. while(!queue.isEmpty() && num[queue.peekLast()]<num[i]){
  18. queue.pollLast();
  19. }
  20. queue.offer(i);
  21. if(i>=size-1){
  22. res.add(num[queue.peek()]);
  23. }
  24. }
  25. return res;
  26. }
  27. }

面试题66:矩阵中的路径

  1. 为什么这么写就不对?
  2. public class Solution {
  3. public boolean hasPath(char[] matrix, int rows, int cols, char[] str)
  4. {
  5. if(matrix==null||rows<1||cols<1||str==null) return false;
  6. boolean[] visited = new boolean[matrix.length];
  7. for(int i=0;i<rows;i++){
  8. for(int j=0;j<cols;j++){
  9. if(dfs(matrix,rows,cols,i,j,0,str,visited)){
  10. return true;
  11. }
  12. }
  13. }
  14. return false;
  15. }
  16. public boolean dfs(char[] matrix,int rows,int cols,int i,int j,int k,char[] str,boolean[] visited){
  17. if(k == str.length - 1){
  18. return true;
  19. }
  20. int index = i*cols+j;
  21. boolean hasPath = false;
  22. if(i>=0&&i<rows&&j>=0&&j<cols&&matrix[index]==str[k]&&visited[index]==false){
  23. visited[index] = true;
  24. hasPath = dfs(matrix,rows,cols,i,j+1,k+1,str,visited)
  25. ||dfs(matrix,rows,cols,i,j-1,k+1,str,visited)
  26. ||dfs(matrix,rows,cols,i+1,j,k+1,str,visited)
  27. ||dfs(matrix,rows,cols,i-1,j,k+1,str,visited);
  28. visited[index] = false;
  29. }
  30. return hasPath;
  31. }
  32. }
  33. 这样写就可以
  34. public class Solution {
  35. public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
  36. int flag[] = new int[matrix.length];
  37. for (int i = 0; i < rows; i++) {
  38. for (int j = 0; j < cols; j++) {
  39. if (helper(matrix, rows, cols, i, j, str, 0, flag))
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. private boolean helper(char[] matrix, int rows, int cols, int i, int j, char[] str, int k, int[] flag) {
  46. int index = i * cols + j;
  47. if (i < 0 || i >= rows || j < 0 || j >= cols || matrix[index] != str[k] || flag[index] == 1)
  48. return false;
  49. if(k == str.length - 1) return true;
  50. flag[index] = 1;
  51. if (helper(matrix, rows, cols, i - 1, j, str, k + 1, flag)
  52. || helper(matrix, rows, cols, i + 1, j, str, k + 1, flag)
  53. || helper(matrix, rows, cols, i, j - 1, str, k + 1, flag)
  54. || helper(matrix, rows, cols, i, j + 1, str, k + 1, flag)) {
  55. return true;
  56. }
  57. flag[index] = 0;
  58. return false;
  59. }
  60. }

面试题67:机器人的运动范围

  1. public class Solution {
  2. public int movingCount(int threshold, int rows, int cols)
  3. {
  4. boolean[] visited = new boolean[rows*cols];
  5. int res = count(0,0,rows,cols,threshold,visited);
  6. return res;
  7. }
  8. public int count (int i,int j,int rows,int cols,int threshold,boolean[] visited){
  9. int cnt = 0;
  10. int index = i*cols+j;
  11. if(check(threshold,i,j,rows,cols,visited) == true){
  12. visited[index] = true;
  13. cnt = 1 + count(i+1,j,rows,cols,threshold,visited)
  14. + count(i-1,j,rows,cols,threshold,visited)
  15. + count(i,j+1,rows,cols,threshold,visited)
  16. + count(i,j-1,rows,cols,threshold,visited);
  17. }
  18. return cnt;
  19. }
  20. public boolean check(int threshold,int i,int j,int rows,int cols,boolean[] visited){
  21. int valRow = getVal(i);
  22. int valCol = getVal(j);
  23. if(i>=0&&i<rows&&j>=0&&j<cols&&valRow+valCol<=threshold&&visited[i*cols+j]==false){
  24. return true;
  25. }
  26. return false;
  27. }
  28. public int getVal(int i){
  29. int val = 0;
  30. while(i>0){
  31. val += i%10;
  32. i /= 10;
  33. }
  34. return val;
  35. }
  36. }

剑指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. win7下IIS错误:"无法访问请求的页面,因为该页的相关配置数据无效"的解决方法(转)

    今天新装win7,然后在IIS下布署了一个网站,布署完成后运行,提示如下错误:HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效 ...

  2. Eclipse tomcat插件禁用热加载

    Eclipse中的tomcat插件默认是开户了热加载,只要是修改了java文件一保存,tomcat自动编译.加载.发布,很吃内存. 关闭方法: 打开eclipse,找到server项: 双击打开,修改 ...

  3. SQL Server 2008 数据库通过镜像同步备份(数据库热备)

    数据库镜像运行模式: 异步数据库镜像模式(异步,高性能模式) 同步数据库镜像模式(同步,高安全模式) 参考资料: http://technet.microsoft.com/zh-cn/library/ ...

  4. Windows下配置nginx根目录的问题

    location / { root E:/xampp/htdocs/html5/php/yii2-rest-master/rest/web; index index.html index.htm; } ...

  5. OPTIMIZE TABLE的作用--转载

    当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删 除操作后在数据文件中留下碎片所致.Discuz! 在系统数设置界面提供了数据表优化的功能,可以去除删除操作后留下的数据文件 ...

  6. 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 ...

  7. BMP图片格式

    BMP图片 BMP采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BMP文件所占用的空间很大.BMP文件的图像深度可选lbit.4bit.8bit及24bit和32bit.BMP文 ...

  8. 翻译: TypeScript 1.8 Beta 发布

    原文地址:https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/ 今天,我们发布了 ...

  9. 关于ILDASM.EXE的知识整理

    因为现在用的VS2010,发现,这个工具自己就带着ILDASM.EXE这个反编译工具 具体的查找方式为: C:\Program Files\Microsoft SDKS\Windows\V7.0\bi ...

  10. lua 高级

    io操作: io.input(filename):指定一个输入流,可以是标准输入stdin,也可以是一个文件路径,返回一个文件句柄: io.output(filename):指定一个输出流,可以是标准 ...