算法学习之剑指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,原来在 ...
随机推荐
- Linux安装yum install gcc-c++出错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos...
错误如图: 解决办法: 1.修改配置文件 /etc/resolv.conf,该配置文件如下: 2.输入:gedit resolv.conf,修改配置文件内容如下: 3.然后重启: 4.重新进行安装: ...
- FreeSql (一)入门
FreeSql 是一个功能强大的对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.5+(QQ群:4336577) FreeSql采用MIT开源协议托管 ...
- vue -- vue-cli webpack项目打包后自动压缩成zip文件
用vue2.0开发项目,使用npm run build 命令 ,但是只会生成dist文件夹,以下是生成zip压缩包方法 1,插件安装 webpack插件安装 filemanager-webpack-p ...
- 新手学习Git之在本地使用Git
每个开发人员应该都会一个版本管理工具,在Git和SVN中,我选择以Git,以下是我的一些心得 什么是 Git Git是目前世界上最先进的分布式版本控制系统(没有之一). 一.Git安装 1).linu ...
- Intellij IDEA 2019 + Java Spring MVC + Hibernate学习笔记(1)
之前的技术栈一直是围绕.net 做的,现在.net 技术栈的使用越来越少,越来越窄.好多原来的同事都转Java开发了. 最近公司变动,自己需要重新找个坑,压力山大.好多要求Java技术栈的根本没机会进 ...
- selenium退出语句区别
selenium关闭窗口有两个方法,close与quit,我们稍作研究便知道这两个方法的区别. 1.看源码或API 这是close()的说明: Closes the current window. 关 ...
- Jupyter Notebook安装和使用详情(你不懂我......)
一.Jupyter Notebook是什么? 1.notebook jupyter 简介 Jupyter Notebook是一个开源Web应用程序,允许您创建和共享包含实时代码,方程式,可视化效果和叙 ...
- hbase、pig、hive配置与应用
------------------HBASE---------- [root@iClient~]#sudo yum install hbase #iClient安装Hbase客户端 [root@cM ...
- JVM 调优 - JPS
Java命令学习系列(一)——Jps 2015-04-16 分类:Java 阅读(23993) 评论(7) 阿里大牛珍藏架构资料,点击链接免费获取 jps位于jdk的bin目录下,其作用是显示当前系统 ...
- Hadoop点滴-何时使用hadoop fs、hadoop dfs与hdfs dfs命令
何时使用hadoop fs.hadoop dfs与hdfs dfs命令 编辑 删除 hadoop fs:使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs:只能操作HDFS文件 ...