LeetCode-921 Minimum Add to Make Parentheses Valid Solution (with Java)
Posted sheepcore
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-921 Minimum Add to Make Parentheses Valid Solution (with Java)相关的知识,希望对你有一定的参考价值。
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 }
以上是关于LeetCode-921 Minimum Add to Make Parentheses Valid Solution (with Java)的主要内容,如果未能解决你的问题,请参考以下文章
921. Minimum Add to Make Parentheses Valid
2019年3月27日 921. Minimum Add to Make Parentheses Valid