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[0][2]=1 vector[0][2]=["()"]
dp[1][2]=0 vector[1][2]=["()"]
dp[0][3]=0 vector[1][2]=["()()"]
dp[0][4]=1 vector[0][4]=["()()","(())"]

dp[0][4]=min(dp[0][1]+d[1][4],....dp[0][3]+dp[4][4],dp[1][3])

class Solution {
public:
    vector<string> str[1005][1005];
    int dp[1005][1005];

    vector<string> removeInvalidParentheses(string s) {

        if(s=="")
        {
            str[0][0].push_back("");
            return str[0][0];
        }

        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='('||s[i]==')')
            {
                dp[i][i] = 1;
                str[i][i].push_back("");
            }
            else
            {
                dp[i][i] = 0;
                string ss;
                ss+=s[i];
                str[i][i].push_back(ss);
            }
        }

        for(int l=1;l<s.length();l++)
        {
            for(int i=0;i+l<s.length();i++)
            {
                int j = i+l;

                dp[i][j]=INT_MAX;

                for(int k=i;k<j;k++)
                {
                    if(dp[i][j]>=dp[i][k]+dp[k+1][j])
                    {
                        dp[i][j]=dp[i][k]+dp[k+1][j];
                        str[i][j].clear();
                        for(int p=0;p<str[i][k].size();p++)
                        {
                            for(int q=0;q<str[k+1][j].size();q++)
                            {
                                string ss = str[i][k][p]+str[k+1][j][q];
                                if(std::find(str[i][j].begin(), str[i][j].end(), ss) == str[i][j].end())
                                    str[i][j].push_back(ss);
                            }
                        }

                    }
                }

                if(s[i]=='('&&s[j]==')')
                {
                    if(l==1)
                    {
                        dp[i][j]=0;
                        str[i][j].clear();
                        str[i][j].push_back("()");
                    }
                    else
                    {
                        if(dp[i][j]>=dp[i+1][j-1])
                        {
                            if(dp[i][j]>dp[i+1][j-1])
                                str[i][j].clear();

                            dp[i][j]=dp[i+1][j-1];

                            for(int p=0;p<str[i+1][j-1].size();p++)
                            {
                                string ss = s[i]+str[i+1][j-1][p]+s[j];
                                if(std::find(str[i][j].begin(), str[i][j].end(), ss) == str[i][j].end())
                                    str[i][j].push_back(ss);
                            }
                        }

                    }
                }
                else
                {
                    if(dp[i][j]>=dp[i+1][j-1]+2)
                    {
                        if(dp[i][j]>dp[i+1][j-1]+2)
                            str[i][j].clear();
                         dp[i][j]=dp[i+1][j-1]+2;

                         for(int p=0;p<str[i+1][j-1].size();p++)
                         {
                             string ss = str[i+1][j-1][p];
                             if(std::find(str[i][j].begin(), str[i][j].end(), ss) == str[i][j].end())
                                    str[i][j].push_back(ss);

                         }
                    }
                }
            }
        }

        return str[0][s.length()-1];

    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12433363.html

时间: 2024-10-07 04:58:07

LeetCode 301. Remove Invalid Parentheses(DP)的相关文章

[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][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题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】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

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

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: [&quo

【leetcode】301. Remove Invalid Parentheses

题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示这个区间是不合法的,需要删掉一个右括号:遍历完成后,如果左括号数量大于右括号的数量,那么需要删除左括号,直至两者相等. 代码如下: class Solution(object): def removeInvalidParentheses(self, s): """ :type s