3.LeetCode20 Valid Parentheses 笔记

1:题目描述

Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

判断输入的括号是否匹配

2:题目分析

括号匹配正确的情况下,第一个闭括号与他之前的第一个左括号肯定对应。python中的列表可以删除元素值,所以只需查看列表第一个右括号是否对应它前面的第一个括号,如果对应,删除这对括号;如果不对应,返回错误。最后列表如果是空的,则匹配正确,否则错误。

3:解题过程(代码)

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s_len=len(s) 
        temp=[]   #设置temp列表储存左括号
        i=0
        j=0
        while i<s_len:  #遍历括号
            temp.append(s[i])  #temp列表添加括号
            j=len(temp)-1        #获取当前列表最后一位的索引位置
            if temp[j]==‘)‘:        #指向列表的最后一个元素
                if temp[j-1]==‘(‘:  #如果匹配,删除这对括号
                    del temp[j]
                    del temp[j-1]
                else:
                    break
                    return False
            elif temp[j]==‘]‘:
                if temp[j-1]==‘[‘:
                    del temp[j]
                    del temp[j-1]
                else:
                    return False
            elif temp[j]==‘}‘:
                if temp[j-1]==‘{‘:
                    del temp[j]
                    del temp[j-1]
                else:
                    return False
            i=i+1
        if len(temp)==0:   #判断temp列表是否为空
            return True
        else:
            return False

4:解题收获

因为python列表可以删除元素,与C的数组不同,所以得到了灵感。第一次感受到用人类思维解决问题的快感 ^_^

原文地址:https://www.cnblogs.com/19991201xiao/p/8397499.html

时间: 2024-10-17 23:32:38

3.LeetCode20 Valid Parentheses 笔记的相关文章

LeetCode20:Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

LeetCode20——Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

32. Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses Problem's Link ---------------------------------------------------------------------------- Mean: 给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair<char,int>>类型的stack. 遇到'('进栈; 遇到')'需要分两种情况讨论: 栈顶元素为'(',正好匹配,

Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

LeetCode之“动态规划”:Longest Valid Parentheses

题目链接 题目要求: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another e