题解 | #括号匹配深度#

括号匹配深度

https://www.nowcoder.com/practice/a2d5b1875bb0408384278f40d1f236c9

import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            maxDepthOfSignal(s);
        }
    }

    private static void maxDepthOfSignal(String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        int max = 0;
        for (char ch : chars) {
            if (ch == '(') {
                stack.push(')');
            } else if (ch == ')') {
                max = Math.max(stack.size(), max);
                stack.pop();
            }
        }
        System.out.println(max);
    }
}  
全部评论

相关推荐

04-13 18:10
门头沟学院 Java
想熬夜的小飞象在秋招:被腾讯挂了后爸妈以为我失联了
点赞 评论 收藏
分享
喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
想申请延毕了,找工作找到崩溃,越找就越想摆烂,还有25届的和我一样感受吗?
码农索隆:没事哒,好兄弟,慢慢来,调整心态,车到山前必有路,感到迷茫的时候,多抬头看看
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务