栈Stack

作者: GeekAmI | 来源:发表于2018-07-17 08:22 被阅读8次
  1. FILO
  2. 手动实现版本
//引用动态数组 https://www.jianshu.com/p/5c663b7c7282
public class ArrayStack<E> implements Stack<E> {

    private Array<E> array;

    public ArrayStack() {
        array = new Array<>();
    }

    public ArrayStack(int capacity){
        array = new Array<>(capacity);
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack [");
        for(int i=0; i<getSize(); i++){
            res.append(i);
            if(i != getSize() - 1){
                res.append(",");
            }
        }
        res.append("] top");
        return res.toString();
    }
}
  1. JDK版本
// Vector的子类,所以Stack是线程安全的
public
class Stack<E> extends Vector<E> {
// 具体实现看源码
  1. 应用举例
// JDK版本的Stack
import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        for (int i = 0; i < s.length(); i++) {
            Character c = s.charAt(i);
            if(c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else{
                if(stack.isEmpty())
                    return false;
                Character top = stack.pop();
                if(c == ')' && top != '(')
                    return false;
                if(c == ']' && top != '[')
                    return false;
                if(c == '}' && top != '{')
                    return false;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new Solution().isValid("()"));
        System.out.println(new Solution().isValid("()[]{}"));
        System.out.println(new Solution().isValid("(]"));
        System.out.println(new Solution().isValid("([)]"));
        System.out.println(new Solution().isValid("{[]}"));
    }
}
// 手动实现版本的Stack
class Solution2 {
    public boolean isValid(String s) {
        ArrayStack<Character> stack = new ArrayStack();
        for (int i = 0; i < s.length(); i++) {
            Character c = s.charAt(i);
            if(c == '(' || c == '[' || c == '{'){
                stack.push(c);
            }else{
                if(stack.isEmpty())
                    return false;
                Character top = stack.pop();
                if(c == ')' && top != '(')
                    return false;
                if(c == ']' && top != '[')
                    return false;
                if(c == '}' && top != '{')
                    return false;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new Solution2().isValid("()"));
        System.out.println(new Solution2().isValid("()[]{}"));
        System.out.println(new Solution2().isValid("(]"));
        System.out.println(new Solution2().isValid("([)]"));
        System.out.println(new Solution2().isValid("{[]}"));
    }
}

相关文章

网友评论

      本文标题:栈Stack

      本文链接:https://www.haomeiwen.com/subject/rczipftx.html