zoukankan      html  css  js  c++  java
  • 【leetcode系列】Valid Parentheses

    非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用java自带的Stack类。

    自己实现的栈代码:

    import java.util.LinkedList;
    
    class StackOne {
    	LinkedList<Object> data;
    	int top;
    	int maxSize;
    
    	StackOne(int size) {
    		// TODO Auto-generated constructor stub
    		top = -1;
    		maxSize = 100;
    		data = new LinkedList<Object>();
    	}
    
    	int getElementCount() {
    		return data.size();
    	}
    
    	boolean isEmpty() {
    		return top == -1;
    	}
    
    	boolean isFull() {
    		return top + 1 == maxSize;
    	}
    
    	boolean push(Object object) throws Exception {
    		if (isFull()) {
    			throw new Exception("栈满");
    		}
    		data.addLast(object);
    		top++;
    		return true;
    	}
    
    	Object pop() throws Exception {
    		if (isEmpty()) {
    			throw new Exception("栈空");
    		}
    		top--;
    		return data.removeLast();
    	}
    
    	Object peek() {
    		return data.getLast();
    	}
    }
    

    推断输出是否有效:

    public class Solution {
    
    	public static boolean isValid(String in) {
    		StackOne stackOne = new StackOne(100);
    		boolean result = false;
    		char[] inArray = in.toCharArray();
    		for (char i : inArray) {
    			if (i == '(' || i == '[' || i == '{') {
    				try {
    					stackOne.push(i);
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				continue;
    			}
    			if (i == ')') {
    				if (stackOne.isEmpty()) {
    					result = false;
    				} else {
    					char tmp = 'u0000';
    					try {
    						tmp = (Character) stackOne.pop();
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					if (tmp == '(') {
    						result = true;
    					}
    				}
    			}
    			if (i == ']') {
    				if (stackOne.isEmpty()) {
    					result = false;
    				} else {
    					char tmp = 'u0000';
    					try {
    						tmp = (Character) stackOne.pop();
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					if (tmp == '[') {
    						result = true;
    					}
    				}
    			}
    			if (i == '}') {
    				if (stackOne.isEmpty()) {
    					result = false;
    				} else {
    					char tmp = 'u0000';
    					try {
    						tmp = (Character) stackOne.pop();
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					if (tmp == '{') {
    						result = true;
    					}
    				}
    
    			}
    		}
    		if (!stackOne.isEmpty()) {
    			result = false;
    		}
    		return result;
    	}
    
    	public static void main(String[] args) {
    		System.out.print(isValid("(}"));
    	}
    }
    


  • 相关阅读:
    neo4j︱与python结合的py2neo使用教程
    Neo4j 简介 2019
    110.Java对象的序列化
    109.Java序列流
    108.Java装饰器设计模式
    107.Java中IO流_字符流的缓冲区
    106.Java中IO流_字符流的异常处理
    105.Java中IO流_字符流拷贝文件
    104.Java中IO流_字符流_Writer
    103.Java中IO流_字符流_Reader
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4212655.html
Copyright © 2011-2022 走看看