Minimum number of Parentheses to be added to make it valid
Given a string S of parentheses ‘(‘ or ‘)’ where, . The task is to find minimum number of parentheses ‘(‘ or ‘)’ (at any positions) we must add to make the resulting parentheses string is valid.
Examples:
分析
balance表示左右差,-1时候右边加) +时候左边加(
最后这里答案是bal(多余的正数代表多余左括号), ans代表需要的右括号。 记得每次ans++, bal也要update
Last updated