32. Longest Valid Parentheses
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<RootNamespace>_32._Longest_Valid_Parentheses</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
5
32. Longest Valid Parentheses/Program.cs
Normal file
5
32. Longest Valid Parentheses/Program.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
var sol = new Solution();
|
||||||
|
|
||||||
|
// System.Console.WriteLine(sol.LongestValidParentheses(")()())"));
|
||||||
|
// System.Console.WriteLine(sol.LongestValidParentheses("()(()"));
|
||||||
|
System.Console.WriteLine(sol.LongestValidParentheses("()(())"));
|
||||||
43
32. Longest Valid Parentheses/Solution.cs
Normal file
43
32. Longest Valid Parentheses/Solution.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
struct Item
|
||||||
|
{
|
||||||
|
public char Sym;
|
||||||
|
public int Count;
|
||||||
|
public char? PrevSym;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int LongestValidParentheses(string s)
|
||||||
|
{
|
||||||
|
var stack = new Stack<Item>();
|
||||||
|
var answer = 0;
|
||||||
|
foreach (var ch in s)
|
||||||
|
{
|
||||||
|
if (stack.Count > 0 && (stack.Peek().Sym == '(' || stack.Peek().PrevSym == '(') && ch == ')')
|
||||||
|
{
|
||||||
|
var prev = stack.Pop();
|
||||||
|
var count = 2;
|
||||||
|
if (prev.Sym == '#')
|
||||||
|
{
|
||||||
|
stack.Pop();
|
||||||
|
count += prev.Count;
|
||||||
|
}
|
||||||
|
while (stack.Count > 0 && stack.Peek().Sym == '#')
|
||||||
|
count += stack.Pop().Count;
|
||||||
|
char? prevSym = null;
|
||||||
|
if (stack.Count > 0)
|
||||||
|
prevSym = stack.Peek().Sym;
|
||||||
|
stack.Push(new Item() { Sym = '#', Count = count, PrevSym = prevSym });
|
||||||
|
answer = Math.Max(answer, count);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stack.Push(new Item() { Sym = ch, Count = 0 });
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Leetcode.sln
14
Leetcode.sln
@@ -191,6 +191,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "26. Remove Duplicates from
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31. Next Permutation\31. Next Permutation.csproj", "{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31. Next Permutation\31. Next Permutation.csproj", "{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "32. Longest Valid Parentheses", "32. Longest Valid Parentheses\32. Longest Valid Parentheses.csproj", "{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -1329,6 +1331,18 @@ Global
|
|||||||
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x64.Build.0 = Release|Any CPU
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.ActiveCfg = Release|Any CPU
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.Build.0 = Release|Any CPU
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user