【算法训练】LeetCode#20 有效的括号

一、描述

20. 有效的括号

难度:简单

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true

二、问题

拿到题基本思路就是利用栈的思想,将最后(右)的符号弹出,存入另一个栈中等待配对。

同一个类中函数相互调用不能忽略self....这个问题找了半天.....

第二个问题,list.remove()是根据值移除,pop是根据位置,调用错了....

class Solution:
    def judge(self, ch1, ch2):
        print(ch1)
        print(ch2)
        # ch1,ch2为传入括号
        if (ch1 == '(' and ch2 == ')') or (ch2 == '(' and ch1 == ')'):
            return True
        elif (ch1 == '{' and ch2 == '}') or (ch2 == '{' and ch1 == '}'):
            return True
        elif (ch1 == '[' and ch2 == ']') or (ch2 == '[' and ch1 == ']'):
            return True
        else:
            return False

    def isValid(self, s: str) -> bool:
        # 将传入字符串转换为list
        leftList = list(s)
        # 定义第二个list接受待匹配字符
        rightList = []
        leftLen = len(leftList)
        rightLen = 0
        for c in range(leftLen - 1, -1, -1):  # 逆序弹出原字符串
            # 逆序弹出原字符串
            # 存入rightList
            rightList.append(leftList[c])
            rightLen = len(rightList)
            # rightLen += 1
            if rightLen > 1:
                tempLen1 = rightLen - 1
                tempLen2 = rightLen - 2
                tempChar1 = rightList[rightLen - 1]
                tempChar2 = rightList[rightLen - 2]
                if Solution.judge(rightList[rightLen - 1], rightList[rightLen - 2]):
                    rightList.remove(rightLen - 1)
                    rightList.remove(rightLen - 2)
                    rightLen -= 2

        if rightLen != 0:
            return False
        else:
            return True

哦吼,第三个问题出现了

需要判断括号的正反方向,我虽然是通过:

        if (ch1 == '(' and ch2 == ')') or (ch2 == '(' and ch1 == ')'):
            return True
        elif (ch1 == '{' and ch2 == '}') or (ch2 == '{' and ch1 == '}'):
            return True
        elif (ch1 == '[' and ch2 == ']') or (ch2 == '[' and ch1 == ']'):
            return True
        else:
            return False

这样判断的正反方向,但是如果出现)(,代码依旧认为是正确的....

三、解题

问题都改了就过了,但是成绩是前几道题中最差的...

class Solution:
    def judge(self, ch1, ch2):
        # ch1,ch2为传入括号
        # ch1应开口朝右,ch2朝左
        if ch1 == '(' and ch2 == ')':
            return True
        elif ch1 == '{' and ch2 == '}':
            return True
        elif ch1 == '[' and ch2 == ']':
            return True
        else:
            return False

    def isValid(self, s: str) -> bool:
        # 将传入字符串转换为list
        leftList = list(s)
        # 定义第二个list接受待匹配字符
        rightList = []
        leftLen = len(leftList)
        rightLen = 0
        for c in range(leftLen - 1, -1, -1):  # 逆序弹出原字符串
            # 逆序弹出原字符串
            # 存入rightList
            rightList.append(leftList[c])
            rightLen = len(rightList)
            # rightLen += 1
            if rightLen > 1:
                if Solution.judge(self, rightList[rightLen - 1], rightList[rightLen - 2]):
                    rightList.pop(rightLen - 1)
                    rightList.pop(rightLen - 2)
                    rightLen -= 2

        if rightLen != 0:
            return False
        else:
            return True

posted @ 2022-01-18 17:43  小拳头呀  阅读(0)  评论(0编辑  收藏  举报