leetcode笔记—Dynamic Programming
10. Regular Expression Matching (hard) #
正则表达式匹配:.和*
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
1 // 递归 2 class Solution { 3 public: 4 bool isMatch(string s, string p) { 5 if (p.empty()) 6 return s.empty(); 7 if(p.size() > 1 && p[1] == '*') 8 return isMatch(s, p.substr(2)) || (!s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p)); 9 else 10 return !s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1)); 11 } 12 };
1 // dp 2 class Solution { 3 public: 4 bool isMatch(string s, string p) { 5 int m = s.size(), n = p.size(); 6 vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false)); 7 dp[0][0] = 1; 8 for (int i = 1; i <= m; ++i) 9 dp[i][0] = 0; 10 for (int i = 2; i <= n; ++i) 11 dp[0][i] = p[i - 1] == '*' && dp[0][i - 2]; 12 for (int i = 1; i <= m; ++i) { 13 for (int j = 1; j <= n; ++j) { 14 if (p[j - 1] == '*') 15 dp[i][j] = dp[i][j - 2] || ((s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]); 16 else 17 dp[i][j] = dp[i - 1][j - 1] && (p[j - 1] == '.' || p[j - 1] == s[i - 1]); 18 } 19 } 20 return dp[m][n]; 21 } 22 };
32. Longest Valid Parentheses (hard) #
最长的有效括号
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.
1 // stack 2 class Solution { 3 public: 4 int longestValidParentheses(string s) { 5 vector<int> st = {-1}; 6 int res = 0; 7 for (int i = 0; i < s.size(); ++i) { 8 if (s[i] == '(') 9 st.push_back(i); 10 else if (!st.empty() && s[st.back()] == '(') 11 st.pop_back(); 12 else 13 st.push_back(i); 14 } 15 st.push_back(s.size()); 16 for (int i = 1; i < st.size(); ++i) 17 res = max(res, st[i] - st[i - 1] - 1); 18 return res; 19 } 20 };
1 // dp 2 class Solution { 3 public: 4 int longestValidParentheses(string s) { 5 int res = 0; 6 vector<int> dp(s.size(), 0); 7 for (int i = 1; i < s.size(); ++i) { 8 if (s[i] == ')') { 9 if (s[i - 1] == '(') 10 dp[i] = i > 2 ? dp[i - 2] + 2 : 2; 11 else if (i - dp[i - 1] - 1 >= 0 && s[i - dp[i - 1] - 1] == '(') 12 dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]; 13 res = max(res, dp[i]); 14 } 15 } 16 return res; 17 } 18 };
44. Wildcard Matching (hard) #
正则表达式匹配:?和*
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*") → true isMatch("aa", "a*") → true isMatch("ab", "?*") → true isMatch("aab", "c*a*b") → false
1 // 递归:runtime error 2 class Solution { 3 public: 4 bool isMatch(string s, string p) { 5 if (p.empty()) 6 return s.empty(); 7 else if (p[0] == '?') 8 return s.empty() ? false : isMatch(s.substr(1), p.substr(1)); 9 else if (p[0] == '*') 10 return isMatch(s, p.substr(1)) || (!s.empty() && isMatch(s.substr(1), p)); 11 else 12 return !s.empty() && s[0] == p[0] && isMatch(s.substr(1), p.substr(1)); 13 } 14 };