国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode -- Wildcard Matching

2019-11-08 02:10:49
字體:
來源:轉載
供稿:網友
題目描述: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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false實現字符串匹配。輸入字符串s,和字符串匹配p,?可被替換為任何非空字符,*可替換空字符或任何字符。設,arr[i,j]表示s的第i個字符是否和p的第j個字符匹配。對于s[i]和p[j],分為三種情況:arr[i,j] = arr[i-1,j-1] ,p[j]=?arr[i,j] = arr[i-1,j-1] && s[i-1] == p[j-1] ,p[j]為普通字符arr[i,j] = arr[i-1,j-1]||arr[i-1,j]||arr[i,j-1] ,p[j]為*實現代碼:
public class Solution {    public bool IsMatch(string s, string p) {        var dp = new bool[s.Length + 1, p.Length + 1];     	dp[0,0]  = true; // s is empty , pattern is empty, match    	    	// s is not empty , patter is empty , not match    	for (var i = 0;i < s.Length; i++){    		dp[i+1,0] = false;    	}    	    	// pattern not empty, s is empty , not match        for (var i = 0;i < p.Length; i++){    		dp[0, i+1] = p[i] == '*' && dp[0, i];    	}    	    	for (var i = 1; i <= s.Length; i++){    		for (var j = 1;j <= p.Length; j++){    			if (p[j-1] == '?'){    				dp[i,j] = dp[i-1,j-1]; // depends on previous match or no    			}    			else if(p[j-1] == '*'){    				// 1. ab a*    				// 2. bavfdc b*    				// pattern j matches string i - 1 (* is any char)    				// or    				// pattern j-1 matches string i (* can be removed)    				//Console.WriteLine(i+","+j);    				dp[i,j] = dp[i-1,j] || dp[i, j-1] || dp[i-1,j-1];    			}    			else{    				// pattern is a normal charactor , previous match also current char should match    				dp[i,j] = dp[i-1,j-1] && s[i-1] == p[j-1];    			}    		}    	}    	//Console.WriteLine(dp);    	return dp[s.Length,p.Length];    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 无极县| 靖宇县| 吉安县| 新宁县| 德令哈市| 砀山县| 孟津县| 襄垣县| 锦屏县| 旌德县| 中阳县| 丽江市| 茶陵县| 湟源县| 延寿县| 政和县| 慈利县| 凌云县| 乌苏市| 凉城县| 慈利县| 鹤岗市| 镇沅| 兴义市| 上饶县| 太和县| 土默特右旗| 日喀则市| 云南省| 东兴市| 安泽县| 余干县| 崇左市| 小金县| 柳河县| 金塔县| 外汇| 蓝田县| 鲁甸县| 光山县| 扶余县|