[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 ...
随机推荐
- 路线更改事件 $routeChangeStart 与 $locationChangeStart
$routeChangeStart属于$route模块,使用将要改变的路由和当前路由对比,在没有跳转之前 参数包括 function(event, next, current) next $loca ...
- 实现TabelView的多个cell布局
- (void)viewDidLoad { [super viewDidLoad]; self.LabelArray=[[NSMutableArray alloc]initWithObjects:@& ...
- 关于调用deleteRowsAtIndexPaths withRowAnimation方法出现错误
通常原因是因为 这个方法的调用与数据源有关. 检测1.你的数据源是否写死了. 2.调用该方法前你是否移除相关的数据源 相关的核心代码如下: - (NSInteger)tableView:(UITabl ...
- devexpress xaf 开发中遇到的问题.
devexpress xaf 开发中遇到的问题很多久了就忘记了.每天都把开发内容记录下来,方便大家,方便自己
- Meta http-equiv的属性详解 来自wanglehui
Meta http-equiv 语法标签格式:<meta http-equiv="参数" content="参数值"> 1."过期时间&q ...
- 手动创建Spring项目 Spring framework
之前学习框架一直是看的视频教程,并且在都配套有项目源码,跟着视频敲代码总是很简单,现在想深入了解,自己从官网下载文件手动搭建,就遇到了很多问题记载如下. 首先熟悉一下spring的官方网站:http: ...
- js中的break ,continue, return (转)
面向对象编程语法中我们会碰到break ,continue, return这三个常用的关键字,那么关于这三个关键字的使用具体的操作是什么呢?我们在使用这三关键字的时候需要注意和需要理解的规则是什么呢? ...
- ajax 选项卡
Ajax其实就是交互式网页应用开发技术包括以下几个方面: 1.XHTML和CSS 2.使用DOM作为动态显示和交互 3.使用XML和XSLT 做数据交互和操作 (这些完全不太懂) 4.使用XMLHtt ...
- Shell基础-环境变量配置文件
Shell基础-环境变量配置文件 source 配置文件 或者 . 配置文件: 把环境变量写入配置文件后,需要用户重新登陆才能生效,而是用source命令,则能直接生效 主要的配置文件: /etc/p ...
- C++ CreateThread 实例
//ThreadBase.h#pragma once #include<windows.h> class CThreadBase { public: CThreadBase(void); ...