LeetCode-921 Minimum Add to Make Parentheses Valid Solution (with Java)

我只是一个虾纸丫 提交于 2020-11-25 02:19:01

1. Description: 

Notes: 

2. Examples: 

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-05-07
 3  */
 4 class Solution {
 5     public int minAddToMakeValid(String s) {
 6         Stack<Character> stack = new Stack<Character>();
 7         int addnum = 0;
 8         for(int i = 0; i < s.length(); i++){
 9             char ch = s.charAt(i);
10             switch(ch){
11                 case '(': stack.push(ch); break;
12                 case ')':
13                     if(!stack.isEmpty() && stack.peek() == '(')
14                         stack.pop();
15                     else
16                         addnum += 1;
17                     break;
18                 default:
19                     System.out.println("Invalid Parentheses");
20             }
21         }
22         return addnum + stack.size();
23     }
24 }

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!