题目描述

原题链接

给你一个只包含 ‘(‘ 和 ‘)’ 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

示例 1:
输入:s = “(()”
输出:2
解释:最长有效括号子串是 “()”

示例 2:
输入:s = “)()())”
输出:4
解释:最长有效括号子串是 “()()”

示例 3:
输入:s = “”
输出:0

提示:

  • 0 <= s.length <= 3 * 104
  • s[i] 为 ‘(‘ 或 ‘)’

    个人解法

Javascript

暴力解法,目前是我最优化之后的暴力解法,但是还是超时了

  1. /*
  2. * @lc app=leetcode.cn id=32 lang=javascript
  3. *
  4. * [32] 最长有效括号
  5. */
  6. // @lc code=start
  7. var check = function (s, left, right) {
  8. const arr = [];
  9. let top = -1;
  10. if (s[left] === ')' || s[right] === '(') {
  11. return false;
  12. }
  13. for (let i = left; i <= right; i++) {
  14. if(top)
  15. if (top > -1 && arr[top] === '(' && s[i] === ')') {
  16. arr.pop();
  17. top--;
  18. } else {
  19. arr.push(s[i]);
  20. top++;
  21. }
  22. }
  23. return arr.length === 0;
  24. }
  25. /**
  26. * @param {string} s
  27. * @return {number}
  28. */
  29. var longestValidParentheses = function (s) {
  30. if (s.length < 2) {
  31. return 0;
  32. }
  33. let left = 0;
  34. let right = s.length - 1;
  35. while (s[left] !== '(' && left < s.length) left++;
  36. while (s[right] !== ')' && right > 0) right--;
  37. let max = 0;
  38. while (left <= right) {
  39. let i = left;
  40. if(s[left] === ')'){
  41. left ++;
  42. continue;
  43. }
  44. if (max > right - i + 1) {
  45. break;
  46. }
  47. let j = (right - i + 1) % 2 === 0 ? right : right - 1;
  48. for (; j > i; j -= 2) {
  49. if (max > j - i + 1) {
  50. break;
  51. }
  52. if (check(s, i, j)) {
  53. max = Math.max(max, j - i + 1);
  54. left = j;
  55. break;
  56. }
  57. }
  58. console.log(left);
  59. left ++;
  60. }
  61. return max;
  62. };
  63. // @lc code=end

Java(栈)

  1. class Solution {
  2. public int longestValidParentheses(String s) {
  3. char[] chars=s.toCharArray();
  4. int len=s.length(),max=0,n=-1;
  5. Stack<Integer> stack=new Stack<>();
  6. stack.push(-1);
  7. for (int i=0;i<len;i++){
  8. //()(()()-1
  9. if (chars[i]=='('){
  10. stack.push(i);
  11. }else {
  12. stack.pop();
  13. if (stack.empty()){
  14. stack.push(i);
  15. }else {
  16. max=Math.max(max,i-stack.peek());
  17. }
  18. }
  19. }
  20. return max;
  21. }
  22. }

其他解法

Java

动态规划

  1. class Solution {
  2. public int longestValidParentheses(String s) {
  3. int maxans = 0;
  4. int[] dp = new int[s.length()];
  5. for (int i = 1; i < s.length(); i++) {
  6. if (s.charAt(i) == ')') {
  7. if (s.charAt(i - 1) == '(') {
  8. dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
  9. } else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
  10. dp[i] = dp[i - 1] + ((i - dp[i - 1]) >= 2 ? dp[i - dp[i - 1] - 2] : 0) + 2;
  11. }
  12. maxans = Math.max(maxans, dp[i]);
  13. }
  14. }
  15. return maxans;
  16. }
  17. }

image.png

  1. class Solution {
  2. public int longestValidParentheses(String s) {
  3. int left = 0, right = 0, maxlength = 0;
  4. for (int i = 0; i < s.length(); i++) {
  5. if (s.charAt(i) == '(') {
  6. left++;
  7. } else {
  8. right++;
  9. }
  10. if (left == right) {
  11. maxlength = Math.max(maxlength, 2 * right);
  12. } else if (right > left) {
  13. left = right = 0;
  14. }
  15. }
  16. left = right = 0;
  17. for (int i = s.length() - 1; i >= 0; i--) {
  18. if (s.charAt(i) == '(') {
  19. left++;
  20. } else {
  21. right++;
  22. }
  23. if (left == right) {
  24. maxlength = Math.max(maxlength, 2 * left);
  25. } else if (left > right) {
  26. left = right = 0;
  27. }
  28. }
  29. return maxlength;
  30. }
  31. }

Javascript

动态规划

  1. const longestValidParentheses = (s) => {
  2. let maxLen = 0;
  3. const len = s.length;
  4. const dp = new Array(len).fill(0);
  5. for (let i = 1; i < len; i++) {
  6. if (s[i] == ')') {
  7. if (s[i - 1] == '(') {
  8. if (i - 2 >= 0) {
  9. dp[i] = dp[i - 2] + 2;
  10. } else {
  11. dp[i] = 2;
  12. }
  13. } else if (s[i - dp[i - 1] - 1] == '(') {
  14. if (i - dp[i - 1] - 2 >= 0) {
  15. dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2];
  16. } else {
  17. dp[i] = dp[i - 1] + 2;
  18. }
  19. }
  20. }
  21. maxLen = Math.max(maxLen, dp[i]);
  22. }
  23. return maxLen;
  24. };

  1. /*
  2. * @lc app=leetcode.cn id=32 lang=javascript
  3. *
  4. * [32] 最长有效括号
  5. */
  6. // @lc code=start
  7. /**
  8. * @param {string} s
  9. * @return {number}
  10. */
  11. const longestValidParentheses = (s) => {
  12. let maxLen = 0;
  13. const len = s.length;
  14. const stack = [-1];
  15. let index = 0;
  16. for (let i = 0; i < len; i++) {
  17. if (s[i] === '(') {
  18. stack.push(i);
  19. index++;
  20. } else {
  21. stack.pop();
  22. index--;
  23. if (index > -1) {
  24. maxLen = Math.max(maxLen, i - stack[index]);
  25. } else {
  26. stack.push(i);
  27. index++;
  28. }
  29. }
  30. }
  31. return maxLen;
  32. };
  33. // @lc code=end