码迷,mamicode.com
首页 > 其他好文 > 详细

Brackets

时间:2015-07-25 16:38:39      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

 1         /// <summary>
 2         /// Solution
 3         /// 通过率 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         }

 

Brackets

标签:

原文地址:http://www.cnblogs.com/HuoAA/p/4675971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!