判断括号字符串是否有效

数组实现栈

 

package cn.bounter.dubbo.suanfa;

import java.lang.reflect.Array;

/**
 * @Description: 使用数组实现栈
 * @Date 2020/5/25
 * @Version V1.0
 **/
public class MyArrayStack<T> {

    private T[] mArray;

    private int count;


    public MyArrayStack(Class<T> type, int count) {
        this.mArray = (T[]) Array.newInstance(type,count);
        this.count = 0;
    }

    public MyArrayStack(Class<T> type) {
        this(type, 12);
    }

    // 将val添加到栈中
    public void push(T val) {
        mArray[count++] = val;
    }

    // 返回“栈顶元素值”
    public T peek() {
        return mArray[count-1];
    }

    // 返回“栈顶元素值”,并删除“栈顶元素”
    public T pop() {
        T ret = mArray[count-1];
        count--;
        return ret;
    }

    // 返回“栈”的大小
    public int size() {
        return count;
    }

    // 返回“栈”是否为空
    public boolean isEmpty() {
        return size()==0;
    }

    public static void main(String[] args) {




    }


}

 

package cn.bounter.dubbo.suanfa;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 括号匹配
 * @Date 2020/5/25
 * @Version V1.0
 **/
public class KuoHaoMatch {


    public static void main(String[] args) {
        String case1="()";
        //输出: true
        String case2="([])";
        //输出: true
        String case3="()[]{}";
        //输出: true
        String case4="])(]";
        //输出: false
        String case5="(]";
        //输出: false
        String case6="([)]";
        //输出: false
        String case7="(([])";


        System.out.println(isValid(case1));
        System.out.println(isValid(case2));
        System.out.println(isValid(case3));
        System.out.println(isValid(case4));
        System.out.println(isValid(case5));
        System.out.println(isValid(case6));
        System.out.println(isValid(case7));

    }

    private static boolean isValid(String case1) {
        Map<Character,Character> map =new HashMap();
        map.put(')','(');
        map.put(']','[');
        map.put('}','{');

        MyArrayStack stack=new MyArrayStack(Character.class);
        for (Character temp: case1.toCharArray()) {
            if (map.containsKey(temp)){
                //1、case4直接为空
                if(stack.isEmpty()){
                    return false;
                }
                Character peek = (char) stack.peek();
                if(peek.equals(map.get(temp))){
                    stack.pop();
                }else {
                    //2、case5,case6直接为空
                    return false;
                }
            }else {
                stack.push(temp);
            }
        }
        if(stack.isEmpty()){
            return true;
        }else {
            //3、case7
            return false;
        }

    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值