[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 ...
随机推荐
- [转]程序员自己写的神器 MonoDevelop 4 (Xamarin Studio) Debugging for Unity
原文地址 http://www.cliffordroche.ca/monodevelop-4-xamarin-studio-debugging-in-unity/ MonoDevelop 4 (Xam ...
- 手机web页面开发-第一弹
毕业设计题目<基于three.js的太阳系全景漫游>,项目开发运行在手机端,开始学习手机端页面开发. 新建index.html,写meta标签.meta标签分为两大部分:http标题信息( ...
- ArrayList添加新元素的覆盖问题
首先,看一个代码段: 1. ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String&g ...
- C++复数类对除法运算符 / 的重载
C8-1 复数加减乘除 (100.0/100.0 points) 题目描述 求两个复数的加减乘除. 输入描述 第一行两个double类型数,表示第一个复数的实部虚部 第二行两个double类型数,表示 ...
- Native VS React Native VS 微信小程序
随着React Native和 微信小程序的出现,Native一家独大的局面出现裂痕,很多小公司使用已经正在着手微信小程序和React Native了,我公司就已经走上React Native之路.那 ...
- jdk环境配置
设置成用户变量就行,无需设置成系统变量. 1.在新弹出窗口上,点系统变量区域下面的新建按钮,弹出新建窗口,变量名为JAVA_HOME,变量值填JDK安装的最终路径,我这里装的地址是D:\Program ...
- 原创: How to build a query based on Definition Updates installed
In SCCM 2012 R2, you can use following class. Use SMS_CombinedDeviceResources.EPAntivirusSignatureLa ...
- linux sed的使用
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理, 可以将数据行进行替换.删除.新增.选取等特定工作. sed本质上是一个编辑器,但是它是非交互式的,这点与VIM不同:同时 ...
- 系统中异常公共处理模块 in spring boot
最近在用spring boot 做微服务,所以对于异常信息的 [友好展示]有要求,我设计了两点: 一. 在业务逻辑代码中,异常的抛出 我做了限定,一般只会是三种: 1. OmcException // ...
- Linux 压缩和解压缩常用命令
主要记录tar,zip,gzip,bzip2,rar等常用命令,对.tar..gz..tar.gz..tgz..bz2..tar.bz2..zip..rar这8种压缩文件的操作. 1. tar 命令 ...