C#如何找到字符串中的额外的)或(括号),并用@
C#如何找到额外的C#如何找到字符串中的额外的)或(括号),并用@,c#,C#,C#如何找到额外的)或(字符串中的括号,并将其替换为@ 样本输入 )(more))) ((((more))) ((((more)) (about)((index)(more))) (about)((index)(more)()) (about)(((index)(more) (about)(((index)(more (about)(((index)(more))))) 样本输出 @(more)@@ @(((more))) @@((more)) (about)((index)(more))@
)
或(
字符串中的括号,并将其替换为@
样本输入
)(more)))
((((more)))
((((more))
(about)((index)(more)))
(about)((index)(more)())
(about)(((index)(more)
(about)(((index)(more
(about)(((index)(more)))))
样本输出
@(more)@@
@(((more)))
@@((more))
(about)((index)(more))@
(about)((index)(more)())
(about)@@(index)(more)
(about)@@(index)@more
(about)(((index)(more)))@@
对于一个经典问题来说,这是一个很好的转折点。与任何括号匹配问题一样,我们需要保留一堆不匹配的开放括号,并在找到相应的封闭括号时清除它们 问题中的例子做得很好-它们非常有助于弄清楚确切的行为应该是什么
public static string BalanceBrackets(string input)
{
// First, we'll do a straight pass through the string. Every time we find a '(', we'll
// record it in a stack. Every time we find a ')', we'll check whether there's a
// corresponding '(' in the stack: if there is, we'll pop it; if there isn't, we've
// got an unmatched ')' and we'll replace it with a '@'.
// When we're done, any unmatched '('s will be in the stack. Replace each of these with
// a '@'.
char[] chars = input.ToCharArray();
// Positions of all unmatched open parens
var unmatchedOpens = new Stack<int>();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == '(')
{
unmatchedOpens.Push(i);
}
else if (chars[i] == ')')
{
if (unmatchedOpens.Count > 0)
{
unmatchedOpens.Pop();
}
else
{
chars[i] = '@';
}
}
}
while (unmatchedOpens.Count > 0)
{
int pos = unmatchedOpens.Pop();
chars[pos] = '@';
}
return new string(chars);
}
公共静态字符串平衡括号(字符串输入)
{
//首先,我们将直接遍历字符串。每次找到“(”,我们将
//将其记录在堆栈中。每次找到“')”时,我们都会检查是否存在
//堆栈中对应的“(”:如果有,我们将弹出它;如果没有,我们将弹出它
//有一个不匹配的“'),我们将用“@”替换它。
//完成后,任何不匹配的“(”将出现在堆栈中。用
//一个“@”。
char[]chars=input.ToCharArray();
//所有未匹配的开放paren的位置
var unmatchedOpens=新堆栈();
for(int i=0;i0)
{
取消匹配dopens.Pop();
}
其他的
{
字符[i]='@';
}
}
}
while(unmatchedOpens.Count>0)
{
int pos=unmatchdopens.Pop();
字符[pos]='@';
}
返回新字符串(字符);
}
一个经典问题的巧妙处理。与任何括号匹配问题一样,我们需要保留一堆不匹配的开放括号,并在找到相应的封闭括号时清除它们 问题中的例子做得很好-它们非常有助于弄清楚确切的行为应该是什么
public static string BalanceBrackets(string input)
{
// First, we'll do a straight pass through the string. Every time we find a '(', we'll
// record it in a stack. Every time we find a ')', we'll check whether there's a
// corresponding '(' in the stack: if there is, we'll pop it; if there isn't, we've
// got an unmatched ')' and we'll replace it with a '@'.
// When we're done, any unmatched '('s will be in the stack. Replace each of these with
// a '@'.
char[] chars = input.ToCharArray();
// Positions of all unmatched open parens
var unmatchedOpens = new Stack<int>();
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == '(')
{
unmatchedOpens.Push(i);
}
else if (chars[i] == ')')
{
if (unmatchedOpens.Count > 0)
{
unmatchedOpens.Pop();
}
else
{
chars[i] = '@';
}
}
}
while (unmatchedOpens.Count > 0)
{
int pos = unmatchedOpens.Pop();
chars[pos] = '@';
}
return new string(chars);
}
公共静态字符串平衡括号(字符串输入)
{
//首先,我们将直接遍历字符串。每次找到“(”,我们将
//将其记录在堆栈中。每次找到“')”时,我们都会检查是否存在
//堆栈中对应的“(”:如果有,我们将弹出它;如果没有,我们将弹出它
//有一个不匹配的“'),我们将用“@”替换它。
//完成后,任何不匹配的“(”将出现在堆栈中。用
//一个“@”。
char[]chars=input.ToCharArray();
//所有未匹配的开放paren的位置
var unmatchedOpens=新堆栈();
for(int i=0;i0)
{
取消匹配dopens.Pop();
}
其他的
{
字符[i]='@';
}
}
}
while(unmatchedOpens.Count>0)
{
int pos=unmatchdopens.Pop();
字符[pos]='@';
}
返回新字符串(字符);
}
所以每一行都是独立的?因为每一行都应该有一组平衡的括号?所以每一行都是独立的?因为每一行都应该有一组平衡的括号?