-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
44 lines (43 loc) · 1.44 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//创建StringBuilder型res,遍历S,如果res不为空,且最后一个字母与S[i]相同,则将res最后一个字母去掉。否则将S[i]加入res。最后返回res.Tostring()。
//用stack可以通用操作,但运行速度会大大降低。
using System;
using System.Collections.Generic;
using System.Text;
namespace RemoveAllAdjacentDuplicatesInString
{
class Program
{
static void Main(string[] args)
{
string S = "abbaca";
Console.WriteLine(RemoveDuplicates1(S));
}
static string RemoveDuplicates1(string S)
{
StringBuilder res = new StringBuilder();
for (int i = 0; i < S.Length; i++)
{
if(res.Length != 0 && res[res.Length - 1] == S[i])
res.Remove(res.Length - 1, 1);
else
res.Append(S[i]);
}
return res.ToString();
}
static string RemoveDuplicates2(string S)
{
Stack<char> container = new Stack<char>();
for (int i = 0; i < S.Length; i++)
{
if (container.Count != 0 && container.Peek() == S[i])
container.Pop();
else
container.Push(S[i]);
}
string res = "";
while (container.Count != 0)
res = container.Pop() + res;
return res;
}
}
}