算法学习之剑指offer(十一)
一
题目描述
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > list = new ArrayList<ArrayList<Integer> >();
int layer=1;
Stack<TreeNode> stack1 = new Stack<>();
stack1.push(pRoot);
Stack<TreeNode> stack2 = new Stack<>();
while(!stack1.empty()||!stack2.empty()){
if(layer%2==1){
ArrayList<Integer> tmp = new ArrayList<Integer>();
while(!stack1.empty()){
TreeNode node = stack1.pop();
if(node!=null){
tmp.add(node.val);
stack2.push(node.left);
stack2.push(node.right);
}
}
if(!tmp.isEmpty())
list.add(tmp);
layer++;
}else{
ArrayList<Integer> tmp = new ArrayList<Integer>();
while(!stack2.empty()){
TreeNode node = stack2.pop();
if(node!=null){
tmp.add(node.val);
stack1.push(node.right);
stack1.push(node.left);
}
}
if(!tmp.isEmpty())
list.add(tmp);
layer++;
}
}
return list;
}
}
二
题目描述
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > list = new ArrayList<ArrayList<Integer> >();
ArrayList<TreeNode> tmpList = new ArrayList<TreeNode>();
if(pRoot==null)
return list;
tmpList.add(pRoot);
while(!tmpList.isEmpty()){
ArrayList<TreeNode> tmpList2 = new ArrayList<TreeNode>();
ArrayList<Integer> tmpInt = new ArrayList<Integer>();
for(int i=0;i<tmpList.size();i++){
tmpInt.add(tmpList.get(i).val);
if(tmpList.get(i).left!=null)
tmpList2.add(tmpList.get(i).left);
if(tmpList.get(i).right!=null)
tmpList2.add(tmpList.get(i).right);
}
list.add(tmpInt);
tmpList = new ArrayList<TreeNode>(tmpList2);
}
return list;
}
}
三
题目描述
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
String Serialize(TreeNode root) {
StringBuffer sb = new StringBuffer();
if(root==null)
{
sb.append("#,");
return sb.toString();
}
sb.append(root.val+",");
sb.append(Serialize(root.left));
sb.append(Serialize(root.right));
return sb.toString();
}
private int index=-1;
TreeNode Deserialize(String str) {
index++;
int len = str.length();
if(index >= len){
return null;
}
String[] strr = str.split(",");
TreeNode node = null;
if(!strr[index].equals("#")){
node = new TreeNode(Integer.valueOf(strr[index]));
node.left = Deserialize(str);
node.right = Deserialize(str);
}
return node;
}
}
四
题目描述
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
private int index=0;
TreeNode resultNode;
TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot == null || k <= 0){
return null;
}
KthNode2(pRoot,k);
return resultNode;
}
void KthNode2(TreeNode pRoot, int k){
if(pRoot == null){
return;
}
KthNode(pRoot.left,k);
index++;
if(index == k){
resultNode = pRoot;
}
KthNode(pRoot.right,k);
}
}
五
题目描述
import java.util.*;
public class Solution {
private int count=0;
//PriorityQueue默认按自然排序,优先取最小的
private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
private PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
//第偶数个插入minHeap,第奇数个插入maxHeap。maxHeap里的数据都小于minHeap里的
public void Insert(Integer num) {
//因为要保证minHeap里的都比maxHeap大,所以先放入maxHeap,再挑maxHeap里最大的放入minHeap
if (count %2 == 0) {
maxHeap.offer(num);
int filteredMaxNum = maxHeap.poll();
minHeap.offer(filteredMaxNum);
} else {
minHeap.offer(num);
int filteredMinNum = minHeap.poll();
maxHeap.offer(filteredMinNum);
}
count++;
}
public Double GetMedian() {
if (count %2 == 0) {//偶数取中位数得除2
return new Double((minHeap.peek() + maxHeap.peek())) / 2;
} else {//奇数个,说明最后一个插到minHeap里了
return new Double(minHeap.peek());
}
}
}
六
题目描述
import java.util.*;
public class Solution {
public ArrayList<Integer> maxInWindows(int [] num, int size)
{
ArrayList<Integer> list = new ArrayList<Integer>();
if(num==null||num.length==0||size<=0)
return list;
int i=0,length=num.length;
while((i+size)<=length){
list.add(maxInNums(num,i,i+size-1));
i++;
}
return list;
}
public int maxInNums(int [] num, int start,int end)
{
int maxnum=num[start];
for(int i=start+1;i<=end;i++){
if(maxnum<num[i])
maxnum=num[i];
}
return maxnum;
}
}
算法学习之剑指offer(十一)的更多相关文章
- 算法学习之剑指offer(九)
一 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). public class Solution ...
- 算法学习之剑指offer(六)
题目1 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. import java.util.*; public cl ...
- 算法学习之剑指offer(五)
题目1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution ...
- 算法学习之剑指offer(四)
题目1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) /** public class TreeNode { int val = 0; Tree ...
- 算法学习之剑指offer(十二)
一 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩 ...
- 算法学习之剑指offer(十)
一 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...
- 算法学习之剑指offer(八)
题目一 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没 ...
- 算法学习之剑指offer(七)
题目1 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P% ...
- 算法学习之剑指offer(三)
题目1 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在 ...
随机推荐
- NOIP2012 D2 T3 疫情控制 洛谷P1084
题目链接:https://www.luogu.org/problemnew/show/P1084 算法:倍增,二分答案,贪心 + 瞎搞.. 背景:上学长的数论课啥也听不懂,于是前去提高组找安慰.不巧碰 ...
- Net基础篇_学习笔记_第十二天_面向对象继承(命名空间 、值类型和引用类型)
命名空间可以认为类是属于命名空间的. 解决类的重名问题,可以看做类的“文件夹”如果在当前项目中没有这个类的命名空间,需要我们手动的导入这个类所在的命名空间.1).用鼠标去点2).alt+shift+F ...
- python实例化时带括号与不带
1.首先这个标题题目不是很准确,但一时又想不到更好的标题所以只好用这个标题,下面我们来看看为什么. 首先我们要明白python中类的实例化是要加上括号的,那么不加括号是什么意思你,看代码 class ...
- [Linux][函数]flock函数的用法
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指 ...
- Java基础系列-深入理解==和equals的区别(一)
一.前言 说到==和equals的问题,面试的时候可能经常被问题到,有时候如果你真的没有搞清楚里边的原因,被面试官一顿绕就懵了,所以今天我们也来彻底了解一下这个知识点. 二.==和equals的作用 ...
- [洛谷日报第39期]比STL还STL?——pbds
[洛谷日报第39期]比STL还STL?——pbds 洛谷科技 发布时间:18-08-3116:37 __gnu_pbds食用教程 引入 某P党:“你们C++的STL库真强(e)大(xin),好多数 ...
- VMbox 安装 LInux系统流程
STEP 1 文件--新建---(自定义高级)---(默认设置)---(稍后安装系统)---(Linux+选择版本)---(虚拟机名字+存放位置)---(处理器2+核数2)---(虚拟机内存)2G一般 ...
- Mybaits-从零开始-Hello World(暂不考虑命名规范化)
1.mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- JsonMessageView工具类
前言 工具类 示例: 前端发送ajax请求 springmvc控制层接收请求并处理请求 前言: 在工作中使用springmvc web框架时常常会发送一个ajax请求,我们在控制层接收到请求并 ...
- mysql having和using使用
1.having当用到聚合函数sum,count后,又需要筛选条件时,就可以考虑使用having,因为where是在聚合前筛选记录的,无法和统计函数一起使用,而having在聚合后筛选记录,可以和统计 ...