【题目描述】
输出有效的括号深度
【代码】
package com.company;标签:count,String,int,max,getKuoHao,括号,LeeCode,自测,stack From: https://blog.51cto.com/u_13682316/5966312
import java.util.*;
class Solution {
public void getKuoHao(String str) {
Stack<Character> stack = new Stack<>();
int max = 0;
int count = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '('){
stack.push(c);
}else if (c == ')' && stack.search('(') == 1 ){
count++;
max = Math.max(max, count);
stack.pop();
if (stack.size() == 0){
count = 0;
}
}
}
System.out.println(max);
}
}
public class Test {
public static void main(String[] args) {
String sss = "()()()"; // 输出1
String str3 = "()(())()"; // 输出2
new Solution().getKuoHao(sss);
new Solution().getKuoHao(str3);
}
}