Leetcode-921 Minimum Add to Make Parentheses Valid(使括号有效的最少添加)

 1 class Solution
 2 {
 3     public:
 4         int minAddToMakeValid(string S)
 5         {
 6             stack<char> s;
 7             int result = 0;
 8             for(auto c:S)
 9             {
10                 if(c=='(')
11                     s.push('(');
12                 else
13                 {
14                     if(s.empty())
15                         result ++;
16                     else
17                     {
18                         s.pop();
19                     }
20                 }
21             }
22             while(!s.empty())
23             {
24                 s.pop();
25                 result ++;
26             }
27             return result;
28         }
29 };

 

posted @ 2018-10-16 18:38  Asurudo  阅读(127)  评论(0编辑  收藏  举报