zoukankan      html  css  js  c++  java
  • Wildcard Matching leetcode java

    题目

    Implement wildcard pattern matching with support for '?' and '*'.

    '?' Matches any single character.
    '*' Matches any sequence of characters (including the empty sequence).
    
    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", "*") → true
    isMatch("aa", "a*") → true
    isMatch("ab", "?*") → true
    isMatch("aab", "c*a*b") → false

    题解
    本文代码引用自:http://blog.csdn.net/perfect8886/article/details/22689147
     1     public boolean isMatch(String s, String p) {  
     2         int i = 0;  
     3         int j = 0;  
     4         int star = -1;  
     5         int mark = -1;  
     6         while (i < s.length()) {  
     7             if (j < p.length()  
     8                     && (p.charAt(j) == '?' || p.charAt(j) == s.charAt(i))) {  
     9                 ++i;  
    10                 ++j;  
    11             } else if (j < p.length() && p.charAt(j) == '*') {  
    12                 star = j++;  
    13                 mark = i;  
    14             } else if (star != -1) {  
    15                 j = star + 1;  
    16                 i = ++mark;  
    17             } else {  
    18                 return false;  
    19             }  
    20         }  
    21         while (j < p.length() && p.charAt(j) == '*') {  
    22             ++j;  
    23         }  
    24         return j == p.length();  
    25     }
     

  • 相关阅读:
    跟踪创建类的个数
    动手动脑3
    动手动脑:随机数发生器和函数重载
    统计英语文章中单词
    动手动脑(1)
    原码、反码、补码
    java测试ATM自助操作系统
    深入浅出 TCP/IP 协议栈
    十大经典排序算法(动图演示)
    深入浅出 Viewport 设计原理
  • 原文地址:https://www.cnblogs.com/springfor/p/3893596.html
Copyright © 2011-2022 走看看