当前位置:网站首页>Brush LeetCode topic series - 10. Regular expression match
Brush LeetCode topic series - 10. Regular expression match
2022-08-02 06:12:00 【Wooden water in the river island】
Given a string s and a character pattern p, please implement a regular expression matching that supports '.' and '*'.
'.' matches any single character
'*' matches zero or more of the preceding elements
The so-called match is to cover the entire string s, not part of the string.
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" cannot match the entire string "aa".
Example 2:
Input: s = "aa", p = "a*"
Output: true
Explanation: Because '*' means it can match zero or more of the preceding elements, here the preceding elementThe element is just 'a'.Therefore, the string "aa" can be treated as 'a' repeated once.
Example 3:
Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" matches zero or more ('*') arbitrary characters ('.').
Source: LeetCode
Link: https://leetcode.cn/problems/regular-expression-matching
Ideas: Big Guy: Classic Dynamic Programming: Regular Expressions :: labuladong's algorithm cheat sheet
Thinking: This question is still a bit difficult.1. Define the function dp((string)s, (string)p, (int)i, (int)j), when dp(s, p, i, j) == true,Indicates that s[i,...] and p[j,...] are regular matches2. For any i and j, s[i] and p[j] have two states, either matching or not matchingThe pseudo code is as follows:if s[i] == p[j] or p[j] = '.' { // when s[i] matches p[j]if j < p.size() - 1 and p[j+1] == '*' {// For * , there are two cases,// 1. p[j] matches the character in s 0 times; 2. p[j] matches the character in s multiple timesbool is_metch_zero = dp(s, p, i, j+2); // p[j] matches the character in s 0 timesbool is_match_one_or_more = dp(s, p, i+1, j); // p[j] matches the character in s multiple timesreturn is_metch_zero || is_match_one_or_more;} else {// When not *, does not involve matching 0 or more timesreturn dp(s, p, i+1, j+1);}} else { // when s[i] does not match p[j]if j < p.size() - 1 and p[j+1] == '*' {return dp(s, p, i, j+2); // match 0 times, continue to look down} else {return false;}}Of course, iteration specifies the case that cannot pass the question. The reason is very simple. Iteration will calculate repeated cases.at this time,We can use a two-dimensional array memp to record the results of midway calculations to avoid repeated case calculations.memp[i][j] indicates whether s[i,...] and p[j,...] are regular matches
The java code is as follows:
class Solution {public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {// Define map, key is a node in the graph, value is (in-degree node, weight value)// to -> (from, price)Map> map = new HashMap<>();for (int[] dists : flights) {int from = dists[0];int to = dists[1];int price = dists[2];int[] subValue = {from, price};if (!map.containsKey(to)) {List inValues = new LinkedList<>();inValues.add(subValue);map.put(to, inValues);} else {map.get(to).add(subValue);}}// record table, record the minimum cost from src to memp[i][j] and the number of transfers is jint[][] memp = new int[n][k + 1];for (int[] value : memp) {Arrays.fill(value, -888);}return dp(src, dst, k+1, map, memp);}int dp(int src, int dst, int k, Map> map, int[][] memp) {if (dst == src) {return 0;}if (k == 0) {return -1;}int res = Integer.MAX_VALUE;// when dst has in-degree nodesif (map.containsKey(dst)) {for (int[] subValues : map.get(dst)) {int from = subValues[0];int price = subValues[1];int fromValue = memp[from][k - 1];if (fromValue == -888) {fromValue = dp(src, from, k - 1, map, memp);memp[from][k - 1] = fromValue;}if (fromValue != -1) {res = Math.min(res, price + fromValue);}}}return res == Integer.MAX_VALUE ? -1 : res;}}
边栏推荐
猜你喜欢
swinIR论文阅读笔记
MySql copies data from one table to another table
CAN光端机解决泰和安TX3016C消防主机长距离联网问题 实现CAN与光纤之间的双向数据智能转换
[Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
【HCIE】NO.30 OSPFv3的基本配置
Mysql子查询关键字的使用(exists)
classSR论文阅读笔记
c语言:查漏补缺(三)
ORA-04044:此处不允许过程、函数、程序包或类型,系统分析与解决
随机推荐
MySQL 5.7 detailed download, installation and configuration tutorial
matlab simulink 模糊pid结合smith控制温度
自动化运维工具——ansible、概述、安装、模块介绍
浏览器的onload事件
Google 安装印象笔记剪藏插件
MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言
matlab simulink 飞机飞行状态控制
Jmeter使用多线程测试web接口
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
CNN 理解神经网络中卷积(大小,通道数,深度)
go项目的打包部署
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
腾讯注册中心演进及性能优化实践
12 reasons for MySQL slow query
力扣 2127. 参加会议的最多员工数 拓扑剪枝与2360补充
swinIR论文阅读笔记
CAN光端机解决泰和安TX3016C消防主机长距离联网问题 实现CAN与光纤之间的双向数据智能转换
Android Studio 实现登录注册-源代码 (连接MySql数据库)
MySQL安装常见报错处理大全