LeetCode 32 括号匹配

[LeetCode 32] Longest Valid Parentheses

题目

Given a string containing just the characters ‘(‘ and ‘)‘, find the length of the longest valid (well-formed) parentheses substring.

测试案例

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"

思路

  1. 采用栈数据结构。栈中存放各字符的下标。初始时里面放入-1。
  2. 从左至右依次遍历每个字符。当前字符为左括号就进栈。当前字符为右括号时,如果栈中存在左括号,则出栈。否则,入栈。
  3. 每当都元素,记下标为 i ,进栈时,就用 i - stack.peek() - 1 更新 max。
  4. 遍历结束后,需要用 n - stack.peek() - 1 更新 max。

代码如下

class Solution {
    public int longestValidParentheses(String s) {
        int max = 0, n = s.length(), temp, index = 0;
        if(n == 0){
            return 0;
        }
        int[] stack = new int[n + 1];
        stack[index++] = -1;
        for(int i = 0; i < n; i++){
            if(s.charAt(i) == ‘(‘ || (temp = stack[index - 1]) == -1 ||
               s.charAt(temp) == ‘)‘){
                if((temp = i - stack[index - 1] - 1) > max){
                    max = temp;
                }
                stack[index++] = i;
            }
            else{
                index--;
            }
        }
        if((temp = n - stack[index - 1] - 1) > max){
            max = temp;
        }
        return max;
    }
}

原文地址:https://www.cnblogs.com/echie/p/9589097.html

时间: 2024-08-29 18:29:44

LeetCode 32 括号匹配的相关文章

leetcode题解: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: Valid Parentheses 有效的括号匹配

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 "(]"

LeetCode 20 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 "([)]&

括号匹配问题(顺序栈实现)

本周老师作业留了两个.先上传一个吧.那个有时间我再传上来~ 本周的要求: 1.给出顺序栈的存储结构定义. 2.完成顺序栈的基本操作函数. 1)      初始化顺序栈 2)      实现入栈和出栈操作 3)      实现取栈顶元素和判空操作 括号匹配问题 3.编写主函数实现基本操作函数功能,并设置测试数据,测试合法和非法数据的输出结果. 4.程序调试运行并保存输出结果. 5.整理并提交实验作业. 1 #include <cstdio> 2 #include <cstring>

堆栈_括号匹配

class Solution { public: bool isValid(string s) { if(s.empty()) return false; stack<int> s1; int n=s.size(); for(int i=0;i<n;i++) { if(s[i]=='('||s[i]=='['||s[i]=='{') s1.push(s[i]); else if(s1.empty()) return false; else if((s[i]==')'&&s

栈的应用-判断括号匹配

栈的一个典型应用就是可以用来协助分析表达式的括号是否匹配.括号可以延伸到任何成对出现的界定符,例如引号,书名号等. 接下来给出程序实现: 第一部分给出的是堆栈的插入,删除等操作对应的实现: 1 public class StackChar 2 { 3 private int maxSize;//堆栈数组大小 4 private char [] stackArray; 5 private int top;//堆栈顶 6 public StackChar(int maxSize) 7 { 8 thi

The application of the stack—Parentheses Matching(括号匹配)

The thought of the algorithm is as follows: (1) Initially set up an empty stack, sequentially read in parentheses; (2) If it is a right parentheses, or it matches the stack top element, or it is illegal; (3) If it is a left parentheses, it will be pu

[栈和队列]括号匹配

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define INIT_STACK_SIZE 100 5 typedef struct 6 { 7 char * chOperator; 8 int dwtop; 9 }OPND; 10 11 void InitStack(OPND *); 12 char Pop(OPND *); 13 void Push(OPND *,char );

括号匹配(二) -- 经典动态规划

这里的括号匹配 , 如果两个相同的话   就执行下面的  语句 if(cmp(str[i],str[j])) dp[i][j] = min(dp[i][j],dp[i+1][j-1]); 每次确定  从 i 到 j 的需要填补的 括号的时候  就默认  这个 值是  105 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<iostream> 5 #include&l