剑指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个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- [JBoss] - JBAS015874: JBoss AS 7.1.1.Final "Brontes" started - Eclipse中不能正常启动的解决方法
在使用Eclipse + Jboss插件时,如果有时写配置文件写错,在重新启动发布JBoss时,会出现: 14:43:49,317 INFO [org.jboss.as] (Controller B ...
- <<精益创业>>读书笔记
不要以严格地职能部门来组成公司,而是要以人们在各自专长的领域做出表现,组建跨部门的团队 在普通的管理中,如果无法实现目标,要么是计划不足,要么是技术不足 这点我感触比较深,以前在老东家的时间,刚开始是 ...
- Mysql 自定义HASH索引带来的巨大性能提升----[真相篇]
推倒重来 俗话说no zuo no die why you try,这时候我又忍不住zuo了,吭哧吭哧的把解决过程发上博客,向全世界宣布,哥又搞定个难题. 剧情的发展往往是看起来主角完全掌握了局势的情 ...
- 【uTenux实验】任务管理
任务就是一个无限循环.uTenux提供的任务管理功能是很强大的,包括建立和删除一个任务,启动或退出任务,取消一个任务的启动请求,改变任务的优先级和査询任务状态,使任务进人睡眠状态和唤醒状态,取消唤醒请 ...
- JavaScript 数据验证类
JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...
- Struts2.3.4+Hibernate4.2.4+Mysql6.0整合
1.项目搭建过程: (1). 创建一个Web Project.导入Struts2和Hibernate的jar包.(如果不知道Struts2的jar包,可以在下载的struts的jar包中,找到apps ...
- 61. Unique Paths && Unique Paths II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- [zhang] ViewController的生命周期分析和使用
iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面是我的一些经验. 一.结构 按结构可以对iOS的所有ViewController分成两类:1.主要用于展示内容的 ...
- libssh2 的集成与应用
http://blog.csdn.net/wyc6668205/article/details/9179197 Xmanager Enterprise 4 putty 等工具都功能都是利用libssh ...
- Python之路【第十七篇】:Django【进阶篇 】
Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...