这一题,测试了好几种情况都通过了,例如:())(;((()));)))(((;(())));但是提交只能通过10%,我现在陷入死角了,求各位大佬帮忙;
我这一题的思路是,遍历一遍字符串,假如最后结果栈中只有")("那么就判断一次交换可以成功,其他情况都不成功
import java.util.Scanner;
import java.util.Stack;
/**
* @Author: Taoyongpan
* @Date: Created in 18:51 2018/4/9
*/
public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int j = 0; j < n; j++) {
String str = sc.nextLine();
Stack stack = new Stack<>();
stack.push(str.charAt(0));
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == '(') {
stack.push(str.charAt(i));
} else if (str.charAt(i)==')'){
if (!stack.isEmpty()) {
if (stack.peek().equals('('))
stack.pop();
else
stack.push(str.charAt(i));
} else
stack.push(str.charAt(i));
}
}
if (stack.size() == 2) {
char c = stack.pop();
if (c=='(' && stack.pop().equals(')'))
System.out.println("Yes");
else
System.out.println("No");
} else
System.out.println("No");
}
}
}