欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java小编程----括号是否匹配

程序员文章站 2024-02-28 19:33:28
...

错误示例::: 当

String s = "...(...[....]....{...}...)...(.[..)..";     应输出3

package com.henu.jihe;
/*
 * String s = "...(...[....]....{...}...)...(...)..";  判断括号是否匹配 
 *	思路:遍历整个String;然后从i=0截取strFirst=s.subString(i,i+1),
 *然后在截取strEnd = s.substring(i+1);先判断strFirst是否为左括号,若是则判断在
 *strEnd中是否包含右括号,若包含则count++;
 */
public class Demo06 {
	public static void main(String[] args) {
		String s = "...(...[....]....{...}...)...(...)..";
		int count = 0;//匹配括号的数目
		for (int i = 0; i < s.length(); i++) {
			String strFirst = s.substring(i,i+1);
			String strEnd = s.substring(i+1);
			if (strFirst.equals("(") && strEnd.contains(")")) {
				count++;
			}else if (strFirst.equals("[") && strEnd.contains("]")) {
				count++;
			}else if (strFirst.equals("{") && strEnd.contains("}")) {
				count++;
			}else {
				continue;
			}
		}
		System.out.println(count);
	}
}

再一个错误示例

package com.henu.jihe;

import java.util.Arrays;
import java.util.Stack;
import java.util.Vector;

/*
 * String s = "...(...[....]....{...}...)...(...)..";  判断括号是否匹配 
 *	思路:遍历整个String;然后从i=0截取strFirst=s.subString(i,i+1),
 *然后在截取strEnd = s.substring(i+1);先判断strFirst是否为左括号,若是则判断在
 *strEnd中是否包含右括号,若包含则count++;
 */
public class Demo06_02 {
	public static void main(String[] args) {
		
		String s = "...(...[....]....{...}...)...(...)..";
		
		String[] str = s.split("\\.+");
		System.out.println(Arrays.toString(str));
		
		
		Stack<String> stack = new Stack<>();
		int count = 0;
		
		for (int i = 0; i < str.length; i++) {
			if ("(".equals(str[i])) {
				stack.push(str[i]);
				if (")".equals(str[i+1])) {
					stack.pop();
					count++;
				}
			}else if ("[".equals(str[i])) {
				stack.push(str[i]);
				if ("]".equals(str[i+1])) {
					stack.pop();
					count++;
				}
			}else if ("{".equals(str[i])) {
				stack.push(str[i]);
				if ("}".equals(str[i+1])) {
					stack.pop();
					count++;
				}
			}else {
				continue;
			}
		}
		
		System.out.println(count);
		
	}
}

 再来个错误示例:::

package com.henu.jihe;

import java.util.Arrays;
import java.util.Stack;

public class Demo06_03 {

    public static void main(String[] args) {

        String s = "...(...[....]....{...}...)...(...)..";

        String[] str = s.split("\\.+");

        System.out.println(Arrays.toString(str));

        Stack<String> stack = new Stack<>();
        int count = 0;
        String string = "";

        for (int i = 0; i+1 < str.length; i++) {
            if ("(".equals(str[i]) || "[".equals(str[i]) || "{".equals(str[i])) {
                stack.push(str[i]);
                string = stack.peek();
            }
//            if (stack.size() != 0) {
//                string = stack.peek();
////                System.out.println(string);
//            }
            if ("(".equals(string) && ")".equals(str[i+1])) {
                stack.pop();
                count++;
            }else if ("[".equals(string) && "]".equals(str[i+1])) {
                stack.pop();
                count++;
            }else if ("{".equals(string) && "}".equals(str[i+1])) {
                stack.pop();
                count++;
            }else {
                continue;
            }
        }
        System.out.println(count);

    }

}

 重头戏:正确的::::

package com.henu.jihe;

import java.util.Arrays;
import java.util.Stack;

public class Demo06_03 {

    public static void main(String[] args) {

        String s = "...(...[....]....{...}...)...(...)..";

        String[] str = s.split("\\.+");

        System.out.println(Arrays.toString(str));

        Stack<String> stack = new Stack<>();
        int count = 0;
        String string = "";

        for (int i = 0; i+1 < str.length; i++) {
            if ("(".equals(str[i]) || "[".equals(str[i]) || "{".equals(str[i])) {
                stack.push(str[i]);
                string = stack.peek();
            }
//            if (stack.size() != 0) {
//                string = stack.peek();
////                System.out.println(string);
//            }
            if ("(".equals(string) && ")".equals(str[i+1])) {
                stack.pop();
                count++;
            }else if ("[".equals(string) && "]".equals(str[i+1])) {
                stack.pop();
                count++;
            }else if ("{".equals(string) && "}".equals(str[i+1])) {
                stack.pop();
                count++;
            }else {
                continue;
            }
        }
        System.out.println(count);

    }

}