[LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.'
and '*'
.
DP:
public class Solution {
public boolean isMatch2(String s, String p) {
int starCnt = 0;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
starCnt++;
}
} boolean[] star = new boolean[p.length() - starCnt];
StringBuilder temp = new StringBuilder(p.length() - starCnt);
int index = -1;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
star[index] = true;
} else {
temp.append(p.charAt(i));
star[++index] = false;
}
}
String r = temp.toString(); boolean[] lastRow = new boolean[s.length() + 1];
boolean[] curRow = new boolean[s.length() + 1];
boolean[] tempRow; lastRow[0] = true;
for (int i = 0; i < r.length(); i++) {
if (star[i] && lastRow[0]) {
curRow[0] = true;
} else {
curRow[0] = false;
} for (int j = 0; j < s.length(); j++) {
if (!star[i]) {
if ((r.charAt(i) == '.' || r.charAt(i) == s.charAt(j)) && lastRow[j]) {
curRow[j + 1] = true;
} else {
curRow[j + 1] = false;
}
} else {
if (lastRow[j + 1]) {
curRow[j + 1] = true;
} else if (lastRow[j] || curRow[j]) {
if (r.charAt(i) == '.' || r.charAt(i) == s.charAt(j)) {
curRow[j + 1] = true;
} else {
curRow[j + 1] = false;
}
} else {
curRow[j + 1] = false;
}
}
} tempRow = lastRow;
lastRow = curRow;
curRow = tempRow;
} return lastRow[lastRow.length - 1];
}
}
[LeetCode] 10. Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- PHPSTORM模板变量注释
类似于这种注释,方便使用. 有两种方式一种是生成PHP文件时,自动生成,一种是手动生成. 第一种:自动生成 一图解释所有.这么配置就OK了. 这种方法还有一种就是,在包括里边编写,直接引用,先写PHP ...
- 胡说REST(REpresentational State Transfer)
Roy T. Fielding的2000年在他的博士论文中提出REpresentational State Transfer这一软件架构风格,相比"表述性状态转移"等等类似的拗口的 ...
- 极路由访问Apple Store可以浏览但是不能下载的解决方案
最近在家里上网突然发现Apple Store不能更新了.重启路由器发现最开始一会是能下载更新的,但是过了一会就完全不能下载更新了.很是奇怪,今天特意分析了一下这个问题. 首先,抓包确定Apple St ...
- 指针数组 null与空字符串
指针数组常适用于指向若干字符串,这样使字符串处理更加灵活方便. 在c++中,null表示:对象为空,它是对指针而言的.而""表示:值为空,它是对字符串而言的.
- 将十六进制的字符串转化为UIImage
最近写一个项目,有验证码,但是接口返回的并不是验证码图片的URL,而是返回的字节数组16进制字符串.这样就需要把16进制字符串首先字节数组,其次再把字节数组转化为NSData,最后再把NSData转化 ...
- C++ 箴言
1.把C++当成一门新的语言学习: 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programming Language ...
- C++类和对象
1.在类体中和类体外定义成员函数是有区别的:在类体中定义的成员函数为内联(inline)函数,在类体外定义的不是.内联函数一般不是我们所期望的,它会将函数调用处用函数体替代,所以我建议在类体内部对成员 ...
- JSP-JSTL学习
<%@page import="com.Student"%> <%@page import="com.Person"%> <%@p ...
- android环境配置
1.计算机右键点击属性 2.点击高级系统设置 3.选择高级——>选择环境变量 4.点击系统变量下的新建 1)新建ANDROID_HOME:你的sdk所在的目录 2)新建JAVA_HOME:C:\ ...
- iOS复选框
这种按钮iOS没有原生效果. 可以靠按钮的不同点击状态来实现这个效果. 代码如下: _workBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [ ...