leetcode2

今天闲来无事又刷了几道。

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 ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

class Solution {
public:
    int longestValidParentheses(string &s) {
        if (s == "") return 0;
        int cnt = 0;
        int totr = 0;
        int lastValid = -1;
        for (int i = 0;i < s.length();i++)
        {
            if (s[i] == ‘)‘) totr++;
            cnt += (s[i] == ‘(‘ ? 1:-1);
            if (cnt < 0)
            {
                if (i+1 < s.length())
                {
                    s = s.substr(i+1,s.length()-i-1);
                    return max(i,longestValidParentheses(s));
                }
                else return i;
            }
            if (cnt == 0) lastValid = i;
        }

        if (lastValid > 0)
        {
            s = s.substr(lastValid+1,s.length()-lastValid-1);
            for (auto& x : s) x = (x==‘(‘?‘)‘:‘(‘);
            reverse(s.begin(),s.end());
            return max(longestValidParentheses(s),lastValid + 1);
        }
        else
        {
            for (auto& x : s) x = (x==‘(‘?‘)‘:‘(‘);
            reverse(s.begin(),s.end());
            return longestValidParentheses(s);
        }
    }
};

只能说,我想歪了。这是道简单的DP,中间还WA了很多次。

我这个神奇的做法也是醉了。

比较需要引起注意的是,递归当中,如果带着数组字符串之类的,如果没有&,很容易MLE。

标准的做法是 f[i] 表示 i 结尾最长的合法序列,

如果(结尾, 0

如果()结尾, f[i-2]+2

如果))结尾, s[i - f[i-1] - 1]==‘(‘ 的情况下 f[i-f[i-1]-2]+f[i-1]+2

时间: 2024-10-15 09:48:42

leetcode2的相关文章

LeetCode2:Median of Two Sorted Arrays

题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 这道题,很容易想到的是先排序再直接定位中间那个数即可,m+n为偶数的话,应为中间两数之和除2,但时间复杂度不符合题目要求,还一种方法就是利用归并思想,因

leetcode2 add two numbers

题目为把两个非负整数逆序存储在链表中,求和并返回一个链表.例如:234+126=360,在这个题目中即为 输入:4-->3-->2    6-->2-->1   输出:0-->6-->3

leetcode2:线性表

/********************************************** Function:input two array and find the kth value the run time cost is O(log(m+n)) **********************************************/ int find_kth(int A[],int m,int B[],int n,int k) { if(m > n) return find_k

leetcode2 Add Two Numbers题解

题的大概意思就是,输入两个列表,这两个列表是两个逆序的数,比如说1->2->4就代表421.然后将两个链表翻转后相加,存入列表中,同样按照逆序存入列表,将其返回,刚开始题意理解错了,WA了两次,题目给出的一组数据比较具有迷惑性,就是243+564与432+465的结果都是807,所以刚开始我以为输入的两个链表的数正序的,只需将结果翻转就可以了.其实这道题和大整数相加差不太多,只要考虑一下进位就没什么问题了.\ 第一版代码如下,比较繁琐,还有一些测试语句: #include <stdio.

[leetcode2]用动态规划解字符串相关的问题

这类问题可以先用递归思考,再反过来想出动态规划的方法. 10. Regular Expression Matching 32. Longest Valid Parentheses 44. Wildcard Matching 72. Edit Distance 97. Interleaving String

[LeetCode-2] Add Two Numbers(链表数据之和)

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

leetcode2 Two Sum II – Input array is sorted

Two Sum II – Input array is sorted [email protected] Question: Similar to Question [1. Two Sum], except that the input array is already sorted in ascending order. 同上题:先排序,然后从开头和结尾同时向中间查找,原理也比较简单.O(nlogn) runtime, O(1) space vector<int> twoSumSored(v

LeetCode2——Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

leetcode--2

#You are given two linked lists representing two non-negative numbers. The digits are stored in #reverse order and each of their nodes contain a single digit. Add the two numbers and return  #it as a linked list. #Input: (2 -> 4 -> 3) + (5 -> 6 -