LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号

https://leetcode-cn.com/problems/remove-outermost-parentheses/

Java Solution

 1 class Solution {
 2     public String removeOuterParentheses(String S) {
 3         char[] chars = S.toCharArray();
 4         int flag = 0;
 5         StringBuilder result = new StringBuilder();
 6
 7         for (char c : chars) {
 8             if (c == ‘(‘) {
 9                 if (flag == 0) {
10                     flag ++;
11                     continue;
12                 }
13                 flag ++;
14             } else if (c == ‘)‘) {
15                 flag --;
16                 if (flag == 0) {
17                     continue;
18                 }
19             }
20             result.append(c);
21         }
22         return result.toString();
23     }
24 }

原文地址:https://www.cnblogs.com/nemowang1996/p/10859483.html

时间: 2024-10-12 03:08:31

LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号的相关文章

LeetCode 1021 Remove Outermost Parentheses

class Solution { public String removeOuterParentheses(String S) { int outer = 0; int inner = 0; char[] intput = S.toCharArray(); char[] output = new char[intput.length]; int i = 0; for (char c: intput) { if (outer != 0) { if (inner == 0) { if (')' ==

LeetCode--To Lower Case & Remove Outermost Parentheses (Easy)

709. To Lower Case(Easy) Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here"

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

[LeetCode][JavaScript]Remove Invalid Parentheses

Remove Invalid Parentheses Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()"

LeetCode.1021-删除最外面的括号(Remove Outermost Parentheses)

这是小川的第380次更新,第408篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第242题(顺位题号是1021).有效的括号字符串为空(""),"("+ A +")"或A + B,其中A和B是有效的括号字符串,+表示字符串连接.例如,"","()","(())()"和"(()(()))"都是有效的括号字符串. 如果有效的括号字符串S是非空

【Leetcode】Remove Invalid Parentheses

题目链接:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parent

[leetcode] 301. Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()" -> ["()()()",

[LeetCode] 301. Remove Invalid Parentheses 移除非法括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples:"()())()" -> ["()()()", &

[leetcode]301. Remove Invalid Parentheses 去除无效括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Example 1: Input: "()())()"Output: ["()()(