2490小蓝的括号串
⭐️难度:中等
🌟考点:栈
📖
📚
import javax.sound.sampled.Line;
import java.math.BigInteger;import java.util.Arrays;
import java.util.Scanner;public class Main {static int N = 105;public static void main(String[] args) {Scanner sc = new Scanner(System.in);int m = sc.nextInt();char []a = new char[N];String str = sc.next();if(m % 2 == 0){ // m 为奇数,永远不可能合法int top = 0;// 把字符串转化成字符数组for (int i = 0; i < m; i++) {char c = str.charAt(i);if(c == '('){a[top] = '(';top ++;} else if (c == ')') {if(top == 0){ // 如果栈到底还有右括号进来,永远不可能合法top = 1;break;}if(a[top - 1] == '('){a[top] = ' ';top --;}else{a[top] = ')';top ++;}}}if(top == 0){System.out.println("Yes");}else{System.out.println("No");}}else{System.out.println("No");}}
}
小细节:YES和Yes