44. Wildcard Matching
来源:互联网 发布:在淘宝上搜血滴子 编辑:程序博客网 时间:2024/05/23 23:22
1刷
难题,第一次在leetcode刷了一道最像acm的题,知道用dp但是想不出来,s和p的遍历先后反了,导致公式写错了很多,哎!!!!
- P[i][j] = P[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == ‘?’), if p[j - 1] != ‘*’;
- P[i][j] = P[i][j - 1] || P[i - 1][j], if p[j - 1] == ‘*’.
2刷要重新刷一次dp,传说中还有贪心的算法,2刷的时候也要刷
Vector初始化vector
class Solution {public: bool isMatch(string s, string p) { vector<vector<bool>> dp(s.length() + 1, vector<bool>(p.length() + 1, false)); dp[0][0] = true; for(int i = 1; i <= p.length(); ++ i){ if(p[i - 1] == '*') dp[0][i] = dp[0][i - 1]; } for(int i = 1; i <= s.length(); ++ i){ for(int j = 1; j <= p.length(); ++ j){ if(p[j - 1] == '*'){ if(dp[i - 1][j] || dp[i][j - 1]) dp[i][j] = true; }else if(dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '?')){ dp[i][j] = true; } } } return dp[s.length()][p.length()]; }};
2刷
2刷的感觉好很多,能够想到基本思路和dp方程!!!!继续3刷这种题,肯定稳健,dp方程在1刷的题解
class Solution {public: bool isMatch(string s, string p) { int n = s.length(); int m = p.length(); vector<vector<bool>>dp(n + 1, vector<bool>(m + 1, false)); dp[0][0] = true; for(int i = 1; i <= m; ++ i) if(p[i - 1] == '*') dp[0][i] = dp[0][i - 1]; for(int i = 1; i <= n; ++ i){ for(int j = 1; j <= m; ++ j){ if(p[j - 1] != '*'){ if(s[i - 1] == p[j - 1] || p[j - 1] == '?') dp[i][j] = dp[i - 1][j - 1]; } else{ dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } } } return dp[n][m]; }};
0 0
- LeetCode --- 44. Wildcard Matching
- [Leetcode] 44. Wildcard Matching
- 44. Wildcard Matching
- Leetcode 44. Wildcard Matching
- 44. Wildcard Matching
- 44. Wildcard Matching
- leetcode 44. Wildcard Matching
- Leetcode 44. Wildcard Matching
- Leetcode:44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- leetcode.44. Wildcard Matching
- LeetCode 44. Wildcard Matching
- Leetcode-44. Wildcard Matching
- 44. Wildcard Matching
- 44. Wildcard Matching
- [LeetCode] 44. Wildcard Matching
- 44. Wildcard Matching
- [leetcode] 44. Wildcard Matching
- 41. First Missing Positive
- struts2中的jsp页面通过iterator调取后台action中的list集合
- Persistent Things
- 42. Trapping Rain Water
- 43. Multiply Strings
- 44. Wildcard Matching
- win10并盘问题
- 45. Jump Game II
- 46. Permutations
- 47. Permutations II
- Quartz 2D 以及手势的综合 小demo
- 48. Rotate Image
- SpringBoot开发详解(一)--初识SpringBoot
- 如何在Github Pages搭建自己写的页面?