国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 湘潭市| 沿河| 汾西县| 泾阳县| 和顺县| 潮安县| 绥中县| 班戈县| 沙河市| 崇明县| 景德镇市| 资兴市| 丹江口市| 荥阳市| 宁武县| 巴林左旗| 巴林右旗| 隆安县| 阳曲县| 宁化县| 柳江县| 望谟县| 林西县| 九台市| 香河县| 朔州市| 东辽县| 札达县| 绥阳县| 平塘县| 莱州市| 武汉市| 家居| 炎陵县| 靖西县| 竹山县| 固原市| 宿松县| 铅山县| 宿松县| 吉木萨尔县|