zoukankan      html  css  js  c++  java
  • 53:正则表达式匹配

    /**
     * 面试题53:正则表达式匹配
     * 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。
     * 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
     */
    public class _53_regular_match {
    	public static void main(String[] args) {
    		Solution53 solution53 = new Solution53();
    		System.out.println(solution53.match("".toCharArray(), ".*".toCharArray()));
    	}
    }
    
    class Solution53 {
    	public boolean match(char[] str, char[] pattern) {
    		if (str == null || pattern == null) {
    			return false;
    		}
    		return getMatch(str, pattern, 0, 0);
    	}
    
    	public boolean getMatch(char[] str, char[] pattern, int index1, int index2) {
    		if ((index1 == str.length && index2 == pattern.length)) {
    			return true;
    		}
    		if (index1 != str.length && index2 == pattern.length) {
    			return false;
    		}
    		if (index2 + 1 < pattern.length && pattern[index2 + 1] == '*') {
    			if ((str.length != index1 && str[index1] == pattern[index2])
    					|| ((str.length != index1 && pattern[index2] == '.'))) {
    				return 	getMatch(str, pattern, index1, index2 + 2) 
    						|| getMatch(str, pattern, index1 + 1, index2 + 2)
    						|| getMatch(str, pattern, index1 + 1, index2);
    			} else {
    				return getMatch(str, pattern, index1, index2 + 2);
    			}
    		}
    		if (index1 < str.length && (str[index1] == pattern[index2] || (pattern[index2] == '.'))) {
    			return getMatch(str, pattern, index1 + 1, index2 + 1);
    		}
    		return false;
    	}
    }
    
  • 相关阅读:
    有效提高生产力的8个贴士
    如何提高团队编程水平
    PHPCMS V9使用中的一些心得体会
    2012全球SEO行业调查报告
    盘点SEO和SEM的优劣势
    DEDECMS 添加栏目图片
    2013年中国500强排行榜(公司名单及网址)
    十大淘宝搜索作弊行为
    利用curl抓取远程页面内容
    新浪,腾讯,淘宝,人人登陆
  • 原文地址:https://www.cnblogs.com/andy-zhou/p/6553668.html
Copyright © 2011-2022 走看看