0
点赞
收藏
分享

微信扫一扫

栈的应用之对称匹配

酷子腿长一米八 2022-01-12 阅读 52

一、判断回文

package p2.线性结构;
//判断回文
public class JudgingPalindrome {
    public static void main(String[] args) {

        System.out.println(solution01());
        System.out.println(solution02());
    }

    //方法二
    //双指针
    private static boolean solution02() {
        String text="上海自来水来自海上";
        int i=0;
        int j=text.length()-1;
        //若循环跳出则说明i>=j,即没有不匹配的,为回文
        //循环中,两两不匹配则直接false,不是回文   匹配继续,直到i>=j
        while (i<j){
            if(text.charAt(i)==text.charAt(j)){
                i++;
                j--;
            }else {
                return false;
            }
        }
        return true;
    }


    //方法一
    //遍历text,当是空 或者 和栈顶不匹配时,直接进栈    不是空和栈顶匹配时,弹栈顶
    //最后看栈是否为空
    //此方法存在bug    如112233的情况,也会判断成是回文
    private static boolean solution01() {
        String text="上海自来水来自海上";
        //i=        0 1 2 3 4 5 67 8
        //text.length()/2=9/2=4
        //即是最中间那个字符时,不管,继续遍历
        ArrayStack<Character> stack=new ArrayStack<>();
        //栈内元素为奇数时
        for (int i=0;i<text.length();i++){
            if(text.length()%2==1 && i==text.length()/2){
                continue;
            }
            char c=text.charAt(i);
            if(stack.isEmpty()){
                stack.push(c);
            }else if (c!= stack.peek()){
                stack.push(c);
            }else {
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}

二、括号匹配

package p2.线性结构;

import java.util.HashMap;
//括号匹配
public class MatchBracket {
    public static void main(String[] args) {
        solution01();
        solution02();
    }

    private static void solution02() {
        String str = "{()[[()]]<>{}()<>}()";
        HashMap<Character, Character> map = new HashMap<>();
        map.put('[', ']');
        map.put('{', '}');
        map.put('<', '>');
        map.put('(', ')');
        ArrayStack<Character> stack = new ArrayStack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (stack.isEmpty()) {
                stack.push(c);
            } else {
                char top = stack.peek();//获取当前栈顶
                if (map.containsKey(top) && c == map.get(top)) {//如果把右边的当成键来看待,就没有匹配的,为空 和c无法比较,报空指针异常 所以map.containsKey(top)
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
        }
        System.out.println(stack.isEmpty());
    }

    private static void solution01() {
        String str = "{()[[()]]<>{}()<>}()";
        ArrayStack<Character> stack = new ArrayStack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (stack.isEmpty()) {
                stack.push(c);
            } else {
                char top = stack.peek();//获取当前栈顶
                if (top - c == -1 || top - c == -2) {//匹配的作差===-1或==-2
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }

        }
        System.out.println(stack.isEmpty());
    }
}
举报

相关推荐

0 条评论