码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-03-02 15:08:12      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:ali   system   img   etc   esc   sem   default   examples   sys   

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)

标签:ali   system   img   etc   esc   sem   default   examples   sys   

原文地址:https://www.cnblogs.com/sheepcore/p/12395292.html

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