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
    



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

  • 相关阅读:
    SpringBoot相知
    SpringBoot初识
    Mybatis 3 配置 Log4j
    IdeaVim-常用操作
    [ZZ] 基于Matlab的标记分水岭分割算法
    [综] meanshift算法
    [ZZ] 麻省理工( MIT)大神解说数学体系
    [ZZ] UIUC同学Jia-Bin Huang收集的计算机视觉代码合集
    公务员 素养
    [zz]有哪些优秀的科学网站和科研软件推荐给研究生?
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4908740.html
Copyright © 2011-2022 走看看