【LeeCode】有效括号深度 (自测)

【题目描述】

输出有效的括号深度

【代码】package com.company;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); }} 生命不是一场赛跑,而是一次旅行。比赛在乎终点,而旅行在乎沿途风景。

【LeeCode】有效括号深度 (自测)

相关文章:

你感兴趣的文章:

标签云: