zoukankan      html  css  js  c++  java
  • 堆叠顺序 和 推断是否成对括号

    public class SqStack {
    
    	public int[] data;
    	public int top;
    	public SqStack() {
    		data = new int[20];
    		top = -1;
    	}
    	public boolean empty() {
    		return top == -1;
    		
    	}
    	public boolean push(int e) {
    		if(top == 19) {
    			return false;
    		}
    		top++;
    		data[top] = e;
    		return true;
    	}
    	public int[] pop() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top--];
    		return arr;
    	}
    	public int[] top() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top];
    		return arr;
    	}
    	public void display() {
    		for(int i=0; i<=top; i++) {
    			System.out.print(data[i]+ " ");
    		}
    		System.out.println();
    	}
    	
    	public static void main(String[] args) {
    		SqStack ss = new SqStack();
    		System.out.println(ss.empty());
    		ss.push(2);
    		ss.push(3);
    		ss.display();
    		ss.pop();
    		ss.display();
    		ss.push(1);
    		ss.push(2);
    		ss.display();
    		ss.pop();
    		ss.push(4);
    		ss.display();
    		ss.top();
    		ss.display();
    
    	}
    
    }
    


    结果:

    true
    2 3 
    2 
    2 1 2 
    2 1 4 
    2 1 4 
    


    public class SqStack {
    
    	public int[] data;
    	public int top;
    	public SqStack() {
    		data = new int[20];
    		top = -1;
    	}
    	public boolean empty() {
    		return top == -1;
    		
    	}
    	public boolean push(int e) {
    		if(top == 19) {
    			return false;
    		}
    		top++;
    		data[top] = e;
    		return true;
    	}
    	public int[] pop() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top--];
    		return arr;
    	}
    	public int[] top() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top];
    		return arr;
    	}
    	public void display() {
    		for(int i=0; i<=top; i++) {
    			System.out.print(data[i]+ " ");
    		}
    		System.out.println();
    	}
    	
    	//检查括号是否配对
    	public boolean check(String s) {
    		for(int i=0; i<s.length(); i++) {
    			if(s.charAt(i) == '(') {
    				push(1);
    			} else if(s.charAt(i) == ')') {
    				if(empty()) 
    					return false;
    				else 
    					pop();
    			}
    		}
    		if(empty()) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	public static void main(String[] args) {
    		SqStack ss = new SqStack();
    		String s = new String("(sd()f(() )d()j)");
    		System.out.println(ss.check(s));
    		s = new String("(sd()f(() ))d()j)");
    		System.out.println(ss.check(s));
    		s = new String("(sd()f((() )d()j)");
    		System.out.println(ss.check(s));
    
    	}
    
    }
    

    结果:

    true
    false
    false
    



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    42.纯 CSS 创作一个均衡器 loader 动画
    41.纯 CSS 绘制一支栩栩如生的铅笔
    1.如何在Cloud Studio上执行Python代码?
    2.每个 HTML 文件里开头都有个<!DOCTYPE>
    39.纯 CSS 创作一个表达怀念童年心情的条纹彩虹心特效
    LOJ #2127. 「HAOI2015」按位或 min-max容斥+FWT
    HDU
    LOJ #3044. 「ZJOI2019」Minimax 搜索 动态DP+概率
    LOJ #3043. 「ZJOI2019」线段树 线段树+分类讨论
    Comet OJ
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4908740.html
Copyright © 2011-2022 走看看