Regular Expression Matching: why the program on my computer right, but on the leetcode...


  • -1
    S
    class Solution {
    public:
    	bool isMatch(const char *s, const char *p) {
    		const char *ss = NULL;
    		if('\0' == *s && '\0' == *p) return true;
    		if(*s == *p){
    			if(*(++p) != '*'){
    				return isMatch(++s, p);
    			}else{
    				while(*p == '*') ++p;
    				ss = s;
    				while(*s == *ss){
    					if(isMatch(s, p)) return true;
    					++s;
    				}
    				if(isMatch(s, p)) return true;
    				else return false;
    			}
    		}
    		if(*p == '.'){
    			if(*(++p) != '*'){
    				return isMatch(++s, p);
    			}else{
    				while(*p == '*') ++p;
    				while(*s != '\0'){
    					if(isMatch(s, p)){
    						return true;
    					}
    					++s;
    				}
    				if(isMatch(s, p)) return true;
    				else return false;
    			}
    		}
    		if(*p != *s){
    			if(*(++p) != '*')return false;
    			while(*p == '*') ++p;
    			return isMatch(s, p);
    		}
    		return false;
    	}
    };

  • 0
    H

    It's not readable, format your code first....


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.