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 (‘)‘ == c) {
                        outer += 1;
                    } else {
                        inner -= 1;
                        output[i] = c;
                        i += 1;
                    }
                } else {
                    if (‘(‘ == c) {
                        inner -= 1;
                        output[i] = c;
                        i += 1;
                    } else {
                        inner += 1;
                        output[i] = c;
                        i += 1;
                    }
                }
            } else {
                outer -= 1;
            }
        }
        return new String(output).substring(0,i);
    }
}

原文地址:https://www.cnblogs.com/stone94/p/10927031.html

时间: 2024-10-31 22:24:07

LeetCode 1021 Remove Outermost Parentheses的相关文章

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

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][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: ["()()(

LeetCode 301. Remove Invalid Parentheses(DP)

题目 DP 险过. dp[i][j] :means it need remove at least dp[i][j] characters to get vaild parenthese from position i to postion j in string. vector str[i][j] store the parenthese string for example : "()())" dp[0][1]=0 vector[0][1]=["()"] dp[