最近被cloudsim折磨慘了,掉進坑裏了~好長時間沒有刷題了,今天不搞TMD的cloudsim了,刷題吧html
這題太沒意思了,直接用java自帶的正則表達式也能經過java
import java.util.regex.Matcher; import java.util.regex.Pattern; class Solution { public boolean isMatch(String s, String p) { Pattern pattern = Pattern.compile(p); Matcher matcher = pattern.matcher(s); return matcher.matches(); } }
固然咱們不能這樣想,要看看有沒有其餘的方法能夠學習正則表達式
https://www.jianshu.com/p/85f3e5a9fcdaspring
http://www.javashuo.com/article/p-pisyjfif-dy.html學習
public boolean isMatch(String s, String p) { if (p.isEmpty()) { return s.isEmpty(); } if (p.length() == 1 || p.charAt(1) != '*') { if (s.isEmpty() || (p.charAt(0) != '.' && p.charAt(0) != s.charAt(0))) { return false; } else { return isMatch(s.substring(1), p.substring(1)); } } //P.length() >=2 while (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')) { if (isMatch(s, p.substring(2))) { return true; } s = s.substring(1); } return isMatch(s, p.substring(2)); }
https://www.nowcoder.com/questionTerminal/d2ccc8cad7234aa5aa68c42471913b86spa
連接:https://www.nowcoder.com/questionTerminal/d2ccc8cad7234aa5aa68c42471913b86 來源:牛客網 public class Demo1 { /* * 若是對動態規劃不太瞭解,能夠參考博客:<a href="http://blog.csdn.net/zjkc050818/article/details/74532023" target="_blank">http://blog.csdn.net/zjkc050818/article/details/74532023 * 以及博客中的視頻。 */ public boolean isMatch(String s, String p) { if (s == null || p == null) return false; int m = s.length(), n = p.length(); boolean[][] res = new boolean[m + 1][n + 1]; res[0][0] = true; for (int i = 0; i < n; i++) { if (p.charAt(i) == '*' && res[0][i - 1]) res[0][i + 1] = true; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (p.charAt(j) == '.') res[i + 1][j + 1] = res[i][j]; if (p.charAt(j) == s.charAt(i)) res[i + 1][j + 1] = res[i][j]; if (p.charAt(j) == '*') { if (s.charAt(i) != p.charAt(j - 1) && p.charAt(j - 1) != '.') res[i + 1][j + 1] = res[i + 1][j - 1]; else { //res[i + 1][j - 1] 表示*一個都不匹配; //res[i + 1][j]表示匹配一個 //res[i][j + 1]表示匹配多個 res[i + 1][j + 1] = res[i + 1][j - 1] || res[i + 1][j] || res[i][j + 1]; } } } } return res[m][n]; } }