Created
September 29, 2024 23:57
-
-
Save unilecs/c884acbca7f93304974f8ddb6e6d811e to your computer and use it in GitHub Desktop.
Задача: Максимальная глубина вложенности скобок
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static int MaxDepth(string s) { | |
int depth = 0; | |
int curDepth = 0; | |
for (int i = 0; i < s.Length; i++) | |
{ | |
curDepth = s[i] == '(' ? curDepth + 1 : s[i] == ')' ? curDepth - 1 : curDepth; | |
depth = Math.Max(depth, curDepth); | |
} | |
return depth; | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
// tests | |
Console.WriteLine(MaxDepth("(4)+((5))+(((6)))")); // 3 | |
Console.WriteLine(MaxDepth("(1+(2*3)+((8)/4))+1")); // 3 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment