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

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

[LeetCode] Regular Expression Matching 解題報告

2019-11-08 01:44:22
字體:
供稿:網(wǎng)友

[題目] mplement 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

[中文翻譯] 實現(xiàn)正則表達式匹配,支持’.’和’*’。

‘.’ 匹配任何單個字符. ‘*’ 匹配0個或多個前導(dǎo)字符.

需要匹配整個輸入的字符串(不是部分).

函數(shù)原型如下: bool isMatch(const char *s, const char *p)

例子: 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

[解題思路] 普通的字符就一一匹配。 ‘.’比較好處理,匹配任意字符。 麻煩的是’*’,使用遞歸,依次枚舉’*’所代表的個數(shù)。

思路大致如此,代碼實現(xiàn)地比較煩,是我很早以前寫的,又沒有寫注釋。 最近很煩,也不想補注釋了,諒解。

[C++代碼]

class Solution {private: string ts, tp; int slen, plen;public: bool isMatch(string s, string p) { ts = s; tp = p; slen = s.size(); plen = p.size(); return myMatch(0, 0); } bool myMatch(int ps, int pp) { if (ps == slen && pp == plen) return true; if (ps < slen && pp == plen) return false; if (pp + 1 < plen) { if ('*' == tp.at(pp + 1)) { for (int i = ps; i < slen; i++) { if (ts.at(i) == tp.at(pp) || '.' == tp.at(pp)) { if (myMatch(i + 1, pp + 2)) return true; } else break; } return myMatch(ps, pp + 2); } else { if (ps == slen) return false; if (ts.at(ps) == tp.at(pp) || '.' == tp.at(pp)) return myMatch(ps + 1, pp + 1); else return false; } } else { if (ps == slen) return false; if (ts.at(ps) == tp.at(pp) || '.' == tp.at(pp)) return myMatch(ps + 1, pp + 1); else return false; } }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会昌县| 鄂伦春自治旗| 中阳县| 喀喇沁旗| 辉南县| 克拉玛依市| 梧州市| 绥化市| 当雄县| 进贤县| 南安市| 武功县| 宜兴市| 安阳市| 江西省| 买车| 东乌| 连州市| 开封市| 衡水市| 青浦区| 沂水县| 太原市| 桃江县| 锦州市| 哈密市| 抚顺市| 兴安盟| 赣州市| 罗甸县| 芷江| 定西市| 舟山市| 鲁山县| 连山| 张北县| 芜湖县| 青海省| 台中县| 阜阳市| 涞源县|