【leetcode】301. Remove Invalid Parentheses

题目如下:

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

代码如下:

class Solution(object):
    def removeInvalidParentheses(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        queue = [(s,0)]
        res = []
        dic = {}
        while len(queue) > 0:
            qs,count = queue.pop(0)
            left = right = 0
            flag = False
            for i,v in enumerate(qs):
                if v == ‘(‘:
                    left += 1
                elif v == ‘)‘:
                    right += 1
                if right > left:
                    for j in range(i+1):
                        if qs[j] == ‘)‘:
                            newqs = qs[:j] + qs[j+1:]
                            if (newqs, count + 1) not in queue:
                                queue.append((newqs,count+1))
                            flag = True
                    break
            if flag == True:
                continue
            if left == right:
                if qs not in dic:
                    dic[qs] = 1
                    if len(res) == 0:
                        res.append((qs,count))
                    else:
                        if res[-1][1] > count:
                            res = [(qs,count)]
                        elif res[-1][1] == count:
                            res.append((qs,count))
                        else:
                            continue
            else:
                for j, v in enumerate(qs):
                    if v == ‘(‘:
                        newqs = qs[:j] + qs[j+1:]
                        if (newqs,count+1) not in queue:
                            queue.append((newqs,count+1))
        ans = []
        for i in res:
            ans.append(i[0])
        return ans

原文地址:https://www.cnblogs.com/seyjs/p/9458691.html

时间: 2024-10-03 06:44:05

【leetcode】301. Remove Invalid Parentheses的相关文章

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

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

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(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[

【LeetCode】203. Remove Linked List Elements

题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 题解: 这道题没什么好讲的,基础操作.需要注意的是链表的第一个node,因为没有前驱节点,所以该node需

【LeetCode】27 - Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution 1: 设置一个新的下标length,遍历过程中遇到val就跳过,否则就赋给新数组下标对应元素.缺