【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

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;

        
    }
};

相关推荐

  1. leetcode

    2024-04-09 15:00:02       39 阅读
  2. leetcode

    2024-04-09 15:00:02       38 阅读
  3. leetcode

    2024-04-09 15:00:02       37 阅读
  4. LeetCode

    2024-04-09 15:00:02       20 阅读
  5. leetcode

    2024-04-09 15:00:02       11 阅读
  6. Leetcode -2

    2024-04-09 15:00:02       35 阅读
  7. Leetcode】计算器

    2024-04-09 15:00:02       42 阅读
  8. LeetCode 45

    2024-04-09 15:00:02       46 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-09 15:00:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-09 15:00:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-09 15:00:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-09 15:00:02       20 阅读

热门阅读

  1. 数据结构_基于顺序表的通讯录

    2024-04-09 15:00:02       12 阅读
  2. 计算机笔记(4)续20个

    2024-04-09 15:00:02       11 阅读
  3. 【Git】tag 标签用法

    2024-04-09 15:00:02       12 阅读
  4. npm指令

    2024-04-09 15:00:02       11 阅读
  5. 【WPF应用42】WPF中的 GroupBox 控件详解

    2024-04-09 15:00:02       12 阅读
  6. Pillow教程:对比两张图片是否相同

    2024-04-09 15:00:02       14 阅读