Brackets

 1         /// <summary>
 2         /// Solution
 3         /// 100/100
 4         /// </summary>
 5         /// <param name="S"></param>
 6         /// <returns></returns>
 7         public static int solution(string S)
 8         {
 9             Stack<char> stack = new Stack<char>();
10             foreach (char ch in S)
11             {
12                 if (stack.Count > 0)
13                 {
14                     if (stack.Peek() == '(' && ch == ')' || stack.Peek() == '[' && ch == ']' || stack.Peek() == '{' && ch == '}')
15                         stack.Pop();
16                     else
17                         stack.Push(ch);
18                 }
19                 else
20                 {
21                     stack.Push(ch);
22                 }
23             }
24             if (stack.Count > 0)
25                 return 0;
26             else
27                 return 1;
28         }

 

posted @ 2015-07-25 15:14  叫我霍啊啊啊  阅读(151)  评论(0编辑  收藏  举报