您的位置:首页 > 其它

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; } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: