4.小蓝的括号串1

4.小蓝的括号串1_第1张图片

题目

import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args) {
		Stack<Character> stack = new Stack<>();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		char[] c = sc.next().toCharArray();
		boolean fa = true;
		for(char x:c) {
			if(x == '(') {
				stack.push(x);
			}else {
				if(!stack.isEmpty()) {
					stack.pop();
				}else {
					fa = false;
					break;
				}
			}
		}
		if(!stack.isEmpty())
			fa = false;
		if(fa)	System.out.println("Yes");
		else	System.out.println("No");
		sc.close();
	}
}

你可能感兴趣的:(蓝桥杯省赛(Java组),java,开发语言)