2016.05.09
2016-05-12 00:00
148 查看
摘要: 刷题第四天
package zuochengyun.chapter1; import java.util.HashMap; import java.util.Stack; import sun.tools.tree.ThisExpression; public class GetMaxTree { public Node getMaxTree(int[] arr) { //将int数组包装成node类型的数组 Node[] narr=new Node[arr.length]; for(int i=0;i stack =new Stack<>(); HashMap lBigMap=new HashMap<>(); HashMap rBigMap=new HashMap<>(); for(int i=narr.length-1;i!=-1;i--){//从右往左 Node curNode=narr[i]; while(!stack.isEmpty() && curNode.value>stack.peek().value){ popStackSetMap(stack,rBigMap); } stack.push(curNode);//如果比栈顶小则压入栈中 } while(!stack.isEmpty()) popStackSetMap(stack,rBigMap); for(int i=0;i!=narr.length;i++){ Node curNode=narr[i]; while(!stack.isEmpty() && curNode.value>stack.peek().value){ popStackSetMap(stack,lBigMap); } stack.push(curNode);//如果比栈顶小则压入栈中 } while(!stack.isEmpty()) popStackSetMap(stack,lBigMap); Node head=null; for(int i=0;i!=narr.length;i++){ Node curNode=narr[i]; Node left=lBigMap.get(curNode); Node right=rBigMap.get(curNode); if(left==null&&right==null) head=curNode; else if(left==null){ if(right.left==null){ right.left=curNode; }else{ right.right=curNode; } }else if(right==null){ if(left.left==null){ left.left=curNode; }else{ left.right=curNode; } }else{ Node parent=left.value stack,HashMap map){ Node popNode=stack.pop(); if(stack.isEmpty()) map.put(popNode, null); else { map.put(popNode, stack.peek()); } } } ////////////////////////////// package zuochengyun.chapter1; import java.util.Stack; public class MaxRecSize { public static int maxRecFromBottom(int[] height) { if(height==null||height.length==0){ return 0; } int maxArea=0; Stack stack=new Stack<>(); for(int i=0;i min=new LinkedList<>(); LinkedList max=new LinkedList<>(); int i=0;int j=0; int res=0; while(i=arr[j]) min.pollLast(); min.addLast(j); while(!max.isEmpty() && arr[max.peekLast()]<=arr[j]) max.pollLast(); max.addLast(j); if(arr[max.peekLast()]-arr[min.peekLast()]>num){ break; } j++; } if(min.peekLast()==i) min.pollFirst(); if(max.peekLast()==i) max.pollFirst(); res+=j-i; } return res; } }
package zuochengyun.chapter1; import java.util.HashMap; import java.util.Stack; import sun.tools.tree.ThisExpression; public class GetMaxTree { public Node getMaxTree(int[] arr) { //将int数组包装成node类型的数组 Node[] narr=new Node[arr.length]; for(int i=0;i stack =new Stack<>(); HashMap lBigMap=new HashMap<>(); HashMap rBigMap=new HashMap<>(); for(int i=narr.length-1;i!=-1;i--){//从右往左 Node curNode=narr[i]; while(!stack.isEmpty() && curNode.value>stack.peek().value){ popStackSetMap(stack,rBigMap); } stack.push(curNode);//如果比栈顶小则压入栈中 } while(!stack.isEmpty()) popStackSetMap(stack,rBigMap); for(int i=0;i!=narr.length;i++){ Node curNode=narr[i]; while(!stack.isEmpty() && curNode.value>stack.peek().value){ popStackSetMap(stack,lBigMap); } stack.push(curNode);//如果比栈顶小则压入栈中 } while(!stack.isEmpty()) popStackSetMap(stack,lBigMap); Node head=null; for(int i=0;i!=narr.length;i++){ Node curNode=narr[i]; Node left=lBigMap.get(curNode); Node right=rBigMap.get(curNode); if(left==null&&right==null) head=curNode; else if(left==null){ if(right.left==null){ right.left=curNode; }else{ right.right=curNode; } }else if(right==null){ if(left.left==null){ left.left=curNode; }else{ left.right=curNode; } }else{ Node parent=left.value stack,HashMap map){ Node popNode=stack.pop(); if(stack.isEmpty()) map.put(popNode, null); else { map.put(popNode, stack.peek()); } } } ////////////////////////////// package zuochengyun.chapter1; import java.util.Stack; public class MaxRecSize { public static int maxRecFromBottom(int[] height) { if(height==null||height.length==0){ return 0; } int maxArea=0; Stack stack=new Stack<>(); for(int i=0;i min=new LinkedList<>(); LinkedList max=new LinkedList<>(); int i=0;int j=0; int res=0; while(i=arr[j]) min.pollLast(); min.addLast(j); while(!max.isEmpty() && arr[max.peekLast()]<=arr[j]) max.pollLast(); max.addLast(j); if(arr[max.peekLast()]-arr[min.peekLast()]>num){ break; } j++; } if(min.peekLast()==i) min.pollFirst(); if(max.peekLast()==i) max.pollFirst(); res+=j-i; } return res; } }
相关文章推荐
- 使用vuejs实现省市联动下拉框
- jQuery:用test方法和keyup方法配合使用正则表达式(初试正则表达式)
- HBase(一):Zookeeper的安装
- HBase(二):HBase安装
- 小谈vi编辑器
- 做大神关于js 动态属性的一些见解 暂时没理解 放着以后看
- 当JS对象属性点后面的属性想用变量表示时代码该如何写
- 【Kafka】Kafka Manager 安装
- 用python 10min手写一个简易的实时内存监控系统
- 用Python写一个简单的监控系统
- python3使用logging日志记录
- 微信第三方授权登录返回错误信息: 40029
- Oracle Logminer 与 rman 官方文档英文
- 版本代码详解:Alpha,Beta,Rc,GA,RTM,OEM等简称都代码什么意义
- Ubuntu配置java环境
- Storyboard使用技巧:使用比例方式布局
- ALAsset和ALAssetRepresentation详解
- android studio使用问题——instant run
- eclipse.ini内存设置
- MySQL主库切换