<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Crackingcodinginterview(3.2)堆棧實現常量復雜度的min函數

        來源:懂視網 責編:小采 時間:2020-11-09 08:11:09
        文檔

        Crackingcodinginterview(3.2)堆棧實現常量復雜度的min函數

        Crackingcodinginterview(3.2)堆棧實現常量復雜度的min函數:3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element Push, pop and min should all operate in O(1) time. 1. Stack1,push(), pop()時間復雜度:O(n) 2.Stack2,與Stack3,滿 3.2 How
        推薦度:
        導讀Crackingcodinginterview(3.2)堆棧實現常量復雜度的min函數:3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element Push, pop and min should all operate in O(1) time. 1. Stack1,push(), pop()時間復雜度:O(n) 2.Stack2,與Stack3,滿 3.2 How

        3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. 1. Stack1,push(), pop()時間復雜度:O(n) 2.Stack2,與Stack3,滿

        3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time.

        1. Stack1,push(), pop()時間復雜度:O(n)

        2.Stack2,與Stack3,滿足要求,Stack3更優化,消除了部分冗余

        3.堆棧特點,在當前元素為彈出時,當前元素的最小È不會改變

        import java.util.Stack;
        
        class Stack1{
        	private Node top = null;
        	private Node first = null;
        	class Node{
        	int val;
        	Node next;
        	Node min;
        	public Node(int val){
        	this.val = val;
        	this.next = null;
        	this.min = null;
        	}
        	}
        	//time complexity:O(n) 
        	public void push(int val){
        	if(top != null){
        	Node n = new Node(val);
        	n.next = top;
        	top = n;
        
        	if(first.val < val){
        	//time complexity:O(n)
        	Node p = null;
        	for(p = first;;p = p.min)
        	if(p.min == null || val < p.min.val){
        	n.min = p.min;
        	p.min = n;
        	break;
        	}
        	
        	}else{
        	n.min = first;
        	first = n;
        	}
        	
        	}else{
        	top = new Node(val);
        	first = top;
        	}
        	}
        	//time complexity:O(n) 
        	public int pop(){
        	if(top != null){
        	Node n = top;
        	top = top.next;
        	
        	Node p = null;
        	if(!n.equals(first)){
        	for(p = first;!n.equals(p.min);p = p.min)
        	;
        	p.min = p.min.min;
        	}else{
        	first = first.min;
        	}	
        	return n.val;
        	}else{
        	return Integer.MIN_VALUE;
        	}
        	}
        	//time complexity:O(1)
        	public int min(){
        	if(first != null)
        	return first.val;
        	else
        	return Integer.MAX_VALUE;
        	}
        	public boolean empty(){
        	if(top == null)
        	return true;
        	else
        	return false;
        	}
        }
        class Stack2{
        	private Node top;
        	class Node{
        	int val;
        	int min;
        	Node next;
        	public Node(int val, int min){
        	this.val = val;
        	this.min = min;
        	this.next = null;
        	}
        	}
        	public void push(int val){
        	if(top != null){
        	Node n = new Node(val, val < top.min ? val : top.min);
        	n.next = top;
        	top = n;
        	}else{
        	top = new Node(val, val);
        	}	
        	}
        	public int pop(){
        	if(top != null){
        	Node n = top;
        	top = top.next;
        	return n.val;
        	}else{
        	return Integer.MIN_VALUE;
        	}
        	}
        	public int min(){
        	if(top != null)
        	return top.min;	
        	else
        	return Integer.MAX_VALUE;
        	}
        	public boolean empty(){
        	if(top == null)
        	return true;
        	else
        	return false;
        	}
        }
        class Stack3{
        	private Node top = null;
        	private Stack s = new Stack();
        	class Node{
        	int val;
        	Node next;
        	public Node(int val){
        	this.val = val;
        	this.next = null;
        	}
        	}
        	public void push(int val){
        	if(top != null){
        	Node n = new Node(val);
        	n.next = top;
        	top = n;
        	
        	if(s.peek() >= val)
        	s.push(val);	
        	}else{
        	top = new Node(val);
        	s.push(val);
        	}
        	}
        	public int pop(){
        	if(top != null){
        	Node n = top;
        	top = top.next;
        	if(n.val == s.peek())
        	s.pop();
        	return n.val;
        	}else
        	return Integer.MIN_VALUE;
        	
        	}
        	public int min(){
        	if(top == null)
        	return Integer.MAX_VALUE;
        	else
        	return s.peek();
        	}
        	public boolean empty(){
        	if(top == null)
        	return true;
        	else
        	return false;
        	}
        }
        public class Solution{
        	public static void main(String[] args){
        	int[] A = {
        	23, 11	, 12, 34, 10, 12, 7, 45, 21,
        	12, 6, 12, 5, 85, 4, 3, 2, 1
        	};
        	//test for Stack1
        	Stack1 stack1 = new Stack1();
        	for(int i=0;i < A.length;i++){
        	stack1.push(A[i]);
        	}
        	while(!stack1.empty()){
        	System.out.print(stack1.pop() + "[" +
        	stack1.min() + "]" + " ");
        	}
        	System.out.println();
        	//test for Stack2
        	Stack2 stack2 = new Stack2();
        	for(int i=0;i < A.length;i++){
        	stack2.push(A[i]);
        	}
        	while(!stack2.empty()){
        	System.out.print(stack2.pop() + "[" +
        	stack2.min() + "]" + " ");
        	}
        	System.out.println();
        	//test for Stack3
        	Stack3 stack3 = new Stack3();
        	for(int i=0;i < A.length;i++){
        	stack3.push(A[i]);
        	}
        	while(!stack3.empty()){
        	System.out.print(stack3.pop() + "[" +
        	stack3.min() + "]" + " ");
        	}
        	System.out.println();
        
        	}
        }
        
        
        
        
        
        
        
        
        
        
        
        
        

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Crackingcodinginterview(3.2)堆棧實現常量復雜度的min函數

        Crackingcodinginterview(3.2)堆棧實現常量復雜度的min函數:3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element Push, pop and min should all operate in O(1) time. 1. Stack1,push(), pop()時間復雜度:O(n) 2.Stack2,與Stack3,滿 3.2 How
        推薦度:
        標簽: 函數 min 堆棧
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top 主站蜘蛛池模板: 99久久免费观看| 最近中文字幕无免费| 国产免费怕怕免费视频观看| 亚洲乱妇熟女爽到高潮的片| 免费看www视频| 国产亚洲视频在线观看| 四虎影在线永久免费四虎地址8848aa| 精品国产亚洲第一区二区三区| 国产一区二区三区免费视频| 又大又硬又粗又黄的视频免费看| 亚洲一区二区三区乱码A| 三年片免费观看大全国语| 亚洲午夜精品一区二区| 一个人免费高清在线观看| 国产精品国产亚洲区艳妇糸列短篇 | 亚洲AⅤ无码一区二区三区在线| 日韩免费高清一级毛片| 国产午夜亚洲精品午夜鲁丝片| 久久aⅴ免费观看| 亚洲精品免费网站| 亚洲日韩精品无码专区网站| 日韩免费在线观看视频| 亚洲最大的黄色网| 又爽又高潮的BB视频免费看| 中文字幕在线视频免费| 亚洲国产夜色在线观看| 亚洲AⅤ无码一区二区三区在线| 免费视频精品一区二区三区| 亚洲综合激情五月丁香六月| 亚洲国产成人精品女人久久久| 久操视频免费观看| 亚洲美国产亚洲AV| 亚洲精品成人片在线观看精品字幕| 免费A级毛片无码A∨免费| 春暖花开亚洲性无区一区二区| 亚洲av午夜福利精品一区人妖| 妞干网在线免费观看| 国产午夜精品免费一区二区三区| 亚洲一本到无码av中文字幕 | 国产精品色拉拉免费看| 免费很黄无遮挡的视频毛片|