Level:

  Medium

题目描述:

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

Example 1:

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

Example 2:

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

思路分析:

  设置一个栈,遍历字符串,如果遇到‘(’,将它对应的下标存放进栈,遇到‘)’,其下标为i,弹出栈顶元素,计算 i-stack.pop()更新res,直到遍历结束,得到最大的res。

代码:

public class Solution{
public int longestValidParentheses(String s){
Stack<Integer>stack=new Stack<>();
int left=-1;
int res=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='(')
stack.push(i);
else{
if(stack.isEmpty())
left=i; //一开始出现‘)’
else{
stack.pop();
if(stack.isEmpty())
res=Math.max(res,i-left);
else
res=Math.max(res,i-stack.peek());
}
}
}
return res;
}
}

62.Longest Valid Parentheses(最长的有效括号)的更多相关文章

  1. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  2. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. 032 Longest Valid Parentheses 最长有效括号

    给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...

  6. 32. Longest Valid Parentheses最长有效括号

    参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...

  7. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  8. Longest Valid Parentheses(最长有效括号)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. elasticsearch 基础 —— URI搜索

    URI搜索 可以通过提供请求参数使用URI来执行搜索请求.使用此模式执行搜索时,并非所有搜索选项都会暴露.这是一个例子: GET twitter/_search?q=user:kimchy 示例响应: ...

  2. ubuntu vim8.1编译安装

    sudo apt-get install libncurses5-dev python-dev python3-dev libgtk-3-dev libatk1.0-dev libbonoboui2- ...

  3. ERROR- 开发常见error

    一,数据插入MySql中出现中文乱码 解决办法有: 1.新建数据库选择 create database 'GG' CHARACTER SET 'utf8 ' COLLATE 'utf8_general ...

  4. 日期格式化:SimpleDateFormat【线程不安全】、FastDateFormat和Joda-Time【后两个都是线程安全】

    SimpleDateFormat是线程不安全的,不能多个线程公用.而FastDateFormat和Joda-Time都是线程安全的,可以放心使用. SimpleDateFormat是JDK提供的,不需 ...

  5. springboot使用异步查询数据

    主要适用于需要查询多种类型的数据,而且二者的参数没有关联的情况. 1.开启异步调用注解 2.创建抽象类,定义相关方法 /** * @author:YZH * time: 2019/8/8 12:16 ...

  6. TCC、XA 、DTP区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11623047.html TCC的优缺点 优点 解决了跨服务的业务操作原子性问题,例如组合支付.下订单减 ...

  7. leetcode-167周赛-1293-网格中的最短路径

    题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: vi ...

  8. Android自动化测试框架UIAutomator原理浅析

    UIAutomator是一个Android自动化测试框架,是谷歌在Android4.1版本发布时推出的一款用Java编写的UI测试框架,它只能用于UI即黑盒方面的测试.所以UIAutomator只能运 ...

  9. .NET Core 通过 Ef Core 操作 Mysql

    1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...

  10. php nl2br()函数 语法

    php nl2br()函数 语法 作用:在字符串中的新行(\n)之前插入换行符dd马达 语法:nl2br(string,xhtml) 参数: 参数 描述 string 必须.规定要检查的字符串. xh ...