[LeetCode][10]Regular Expression Matching解析 -Java實現

Q:

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
A:

以下解法和代碼沒有借閱以往任何資料,如果有更好的解法請在評論區留言

這道題的大意是實現一個規則表達式的匹配。有兩個符號“.”代表任意單個字符”*“代表0或者多個之前的字符。這個我想了想應該可以用隊列做,效率也不錯。大概畫個圖表達一下


首先可以肯定的是遇到.*這種配合肯定可以直接返回了,而且把a*看成一個字符,或者可以這樣說,對比的時候如果字符不一樣,規則表達式下一個字符是*則不返回false並且繼續判斷。如果遇到。則跳過本次判斷(需要斷定下次不是*)

public class RegularExpressionMatching {
	public static void main(String[] args){
		String s = "abc";
		String reg = "ab*c*";
		System.out.println(method( s, reg));
	}

	private static boolean method(String s, String reg) {
		// TODO Auto-generated method stub
		char[] chars = s.toCharArray();
		char[] charreg = reg.toCharArray();
		char charlast = 0;
		int regIndex = 0;//reg遊標
		for(int i =0;i<s.length();i++){//超出遊標範圍
			if(regIndex>=charreg.length)
				return false;
			
			if(charreg[regIndex]=='.'){//得到。直接跳過
				charlast = charreg[regIndex];
				regIndex++;
				continue;
				
			}else if(charreg[regIndex]=='*'){
				if(regIndex!=0){
					if(charlast=='.')//點星匹配直接返回
						return true;
					else {//星號向下匹配
						int j = i;
						for(;j<s.length();j++){
							if(chars[j]!=charlast)
								break;
						}
						charlast = charreg[regIndex];
						regIndex++;
						i=--j;
						continue;
					}
				}
			}else {//得到字符
				if(chars[i]!=charreg[regIndex]){
					if(regIndex<(charreg.length-1)&&charreg[regIndex+1]=='*'){
						regIndex+=2;
						continue;
					}
					return false;
				}
				if(regIndex<(charreg.length-1)&&charreg[regIndex+1]=='*'){
					charlast = charreg[regIndex];
					i--;
					regIndex++;
					continue;
				}
				regIndex++;
			}
			charlast = charreg[regIndex];
		}
		if(regIndex!=charreg.length)//字長不匹配
		{	if(charreg.length>=regIndex){
				if((regIndex+1)==charreg.length){
					if(charreg[regIndex]!='*')
						return false;
					else
						return true;
				}
				if(charreg[charreg.length-1]!='*')
					return false;
				for(int i = regIndex+2;i<charreg.length;i++){//餘下字符都是.*或者char*才行
					if(charreg[i-1]!='.'&&charreg[i-1]!='*'&&charreg[i]!='.'&&charreg[i]!='*'){
						return false;
					}
				}
			}
			return false;
		}
		return true;
	}

由於用了一種笨方法,分叉判斷較多可能有誤,如果有誤請告知,謝謝~也因爲這次代碼比較雜亂,我可能會跟着有一個改進版,容我最近想想,參考其他篇目。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章