【Leetcode】【240406】1249. Minimum Remove to Make Valid Parentheses

其实大部分是东京时间第二天凌晨才做的- -但国际服刷新比较晚

BGM:刀剑如梦

Decsripition

Given a string s of '(' , ')' and lowercase English characters.

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

Formally, a parentheses string is valid if and only if:

  • It is the empty string, contains only lowercase characters, or
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

Example

Example 1:

Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.

Example 2:

Input: s = "a)b(c)d"
Output: "ab(c)d"

Example 3:

Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.

Solution

Just use stack to pair left and right parentheses, but remeber to save the num-th of the remain char.

Code

cpp 复制代码
class Solution {
public:
    string minRemoveToMakeValid(string s)
    {
        typedef struct pair
        {
            char kyara;
            int num;
        };
        stack<pair>tree;
        bool judge[100005]={0};
        for(int i=0;i<s.length();++i)
        {
            if(s[i]=='(')
            {
                pair mid;
                mid.kyara=s[i];
                mid.num=i;
                tree.push(mid);
            }
            else if(s[i]==')')
            {
                if(!tree.empty())
                {
                    pair mid=tree.top();
                    if(mid.kyara=='(') tree.pop();
                    else
                    {
                        pair curr;
                        curr.kyara=s[i];
                        curr.num=i;
                        tree.push(curr);
                    }
                }
                else
                {
                    pair mid;
                    mid.kyara=s[i];
                    mid.num=i;
                    tree.push(mid);
                }
            }
        }
        while(!tree.empty())
        {
            pair mid=tree.top();
            judge[mid.num]=1;
            tree.pop();
        }
        string c="";
        for(int i=0;i<s.length();++i)
        {
            if(judge[i]==0) c.push_back(s[i]);
        }
        return c;

        
    }
};
相关推荐
Swift社区7 小时前
LeetCode - #139 单词拆分
算法·leetcode·职场和发展
Dong雨9 小时前
力扣hot100-->栈/单调栈
算法·leetcode·职场和发展
trueEve10 小时前
SQL,力扣题目1369,获取最近第二次的活动
算法·leetcode·职场和发展
九圣残炎12 小时前
【从零开始的LeetCode-算法】3354. 使数组元素等于零
java·算法·leetcode
程序猿小柒12 小时前
leetcode hot100【LeetCode 4.寻找两个正序数组的中位数】java实现
java·算法·leetcode
_OLi_13 小时前
力扣 LeetCode 106. 从中序与后序遍历序列构造二叉树(Day9:二叉树)
数据结构·算法·leetcode
我明天再来学Web渗透14 小时前
【SQL50】day 2
开发语言·数据结构·leetcode·面试
小叶lr15 小时前
idea 配置 leetcode插件 代码模版
java·leetcode·intellij-idea
理论最高的吻18 小时前
98. 验证二叉搜索树【 力扣(LeetCode) 】
数据结构·c++·算法·leetcode·职场和发展·二叉树·c
沈小农学编程18 小时前
【LeetCode面试150】——202快乐数
c++·python·算法·leetcode·面试·职场和发展