- 浏览: 137632 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
[balabala] dp[i][j] 表示p的前i个字符能否匹配s的前j个字符。分两大类情况讨论:
1)p的当前字符 != '*': dp[i][j] = dp[i - 1][j - 1] && (currP == currS || currP == '.');
2) p的当前字符 == '*': dp[i][j] 为true的条件是星号要么匹配0次,要么匹配1次,要么匹配多次。匹配多次时根据p的前一个字符分为两种情况:前字符是'.'对s的当前字符无限制, 前字符非 '.'时要求s的当前字符和p的前字符相同
此外,注意到dp[i][0] = dp[i - 2][0] && currp == '*'
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
[balabala] dp[i][j] 表示p的前i个字符能否匹配s的前j个字符。分两大类情况讨论:
1)p的当前字符 != '*': dp[i][j] = dp[i - 1][j - 1] && (currP == currS || currP == '.');
2) p的当前字符 == '*': dp[i][j] 为true的条件是星号要么匹配0次,要么匹配1次,要么匹配多次。匹配多次时根据p的前一个字符分为两种情况:前字符是'.'对s的当前字符无限制, 前字符非 '.'时要求s的当前字符和p的前字符相同
此外,注意到dp[i][0] = dp[i - 2][0] && currp == '*'
public boolean isMatch(String s, String p) { if (s == null || p == null) return false; int lengthS = s.length(); int lengthP = p.length(); boolean[][] dp = new boolean[lengthP + 1][lengthS + 1]; dp[0][0] = true; for (int i = 1; i <= lengthP ; i++) { char currP = p.charAt(i - 1); if (i >= 2) { dp[i][0] = dp[i - 2][0] && currP == '*'; } else { dp[i][0] = false; } for (int j = 1; j <= lengthS; j++) { if (currP != '*') { dp[i][j] = dp[i - 1][j - 1] && (currP == s.charAt(j - 1) || currP == '.'); } else if(i >= 2) { char lastP = p.charAt(i - 2); dp[i][j] = dp[i - 2][j] || dp[i - 1][j] || (dp[i][j - 1] && (lastP == '.' || lastP == s.charAt(j - 1))); } } } return dp[lengthP][lengthS]; }
发表评论
-
Leetcode - Ugly Number II
2015-08-24 22:54 1163[分析] 暴力的办法就是从1开始检查每个数是否是丑数,发现丑数 ... -
Leetcode - Paint House II
2015-08-20 20:37 1603There are a row of n houses, ea ... -
Leetcode - Maximum Square
2015-08-16 13:33 507Given a 2D binary matrix filled ... -
Leetcode - Paint House
2015-08-16 10:48 1150There are a row of n houses, ea ... -
Leetcode - Different Ways to Add Parentheses
2015-07-29 20:21 1200Given a string of numbers and o ... -
Jump Game II
2015-07-05 16:49 545Given an array of non-negative ... -
Leetcode - Jump Game
2015-07-05 15:52 533Given an array of non-negative ... -
Leetcode - Interleaving String
2015-06-07 11:41 613Given s1, s2, s3, find whe ... -
Leetcode - Wildcard Matching
2015-06-06 20:01 997Implement wildcard pattern ma ... -
Leetcode - Maximal Square
2015-06-04 08:25 619Given a 2D binary matrix fille ... -
Leetcode - Palindrome Partition II
2015-05-21 21:15 684Given a string s, partition ... -
Leetcode - Palindrome Partition
2015-05-21 09:56 786Given a string s, partition s s ... -
Leetcode - House Robber II
2015-05-20 22:34 772Note: This is an extension of ... -
Leetcode - Maximum Rectangle
2015-05-20 08:58 502Given a 2D binary matrix fill ... -
Leetcode - Scramble String
2015-05-17 14:22 583Given a string s1, we may repre ... -
Leetcode - Distinct Subsequences
2015-05-01 16:56 507Given a string S and a string T ... -
Leetcode - Best Time to Buy and Sell Stock IV
2015-05-01 16:11 615Say you have an array for which ... -
Leetcode - Best Time to Buy and Sell Stock IV
2015-04-23 09:59 0public class Solution ... -
Leetcode - Best Time to Buy and Sell Stock III
2015-04-23 09:04 483Say you have an array for whi ... -
Leetcode - Dungeon Game
2015-04-21 09:50 459The demons had captured the pr ...
相关推荐
js js_leetcode题解之10-regular-expression-matching.js
c语言入门 C语言_leetcode题解之10-regular-expression-matching.c
c c语言_leetcode 0010_regular_expression_matching.zip
- Regular Expression Matching: 给定一个输入字符串和一个模式,实现支持'.'和'*'的正则表达式匹配。 - Container With Most Water: 给定一个数组,其中每个元素代表一个宽度为1的柱子高度,要求找出两个柱子,使得...
分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。...Expression Matching 25.3% 困难 11 Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In
lru cache leetcode Leetcode ...Expression Matching 动态规划,列出转换方程即可,注意初值 记T[i][j] = 是否S[0:i]和P[0:j]匹配 再分类讨论,其中pattern *分为0, 1, more三种类型 0: i不变, j+1
35%2☆ ☆27%3☆ ☆24%4☆ ☆ ☆21%5☆ ☆25%6☆ ☆26%7☆24%8☆ ☆13%9Palindrome Number☆35%10Regular Expression Matching☆ ☆ ☆24%:red_heart:11Container With Most Water☆ ☆36%12Integer to R
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
8. Regular Expression Matching in Java 正则表达式匹配是一个字符串问题,要求使用正则表达式来匹配字符串。可以使用Java的Pattern和Matcher类来解决该问题。 9. Merge Intervals 区间合并是一个数组问题,要求...
Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy ,...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
lru cache leetcode leetcode leetcode solutions in C++ 微信公众号:曲奇泡芙 (互联网&车联网技术) ..../0010-regular-expression-matching.cpp ./0011-container-with-most-water.cpp ./0012-int
https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching 100 https://leetcode.com/problems/same-tree/ Same Tree 101 https://leetcode.com/problems/symmetric-tree/ ...
leetcode 答案 leet code 这是我的leetcode答案,语言主要是用python。很多算法不是最优,甚至很槽糕,有空就会优化 目前完成了: Two Sum Add Two Numbers Longest ...Regular Expression Matching
Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 ...
leetcode 跳跃 LeetCode Solved by ...Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman 整数转罗马数字 13. Roman to Integer 罗马数字转
[10_regular-expression-matching.cpp] [100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] [102_binary-tree-level-order-traversal.cpp] [103_binary-tree-zigzag-level-order-traversal.cpp] ...
4. **正则表达式匹配**:“实现一个简单的正则表达式匹配器”(Regular Expression Matching),涉及到递归和动态规划。 5. **字符串编码与解码**:“无重复字符的最长子串”(Longest Substring Without Repeating...
"regular-expression-matching.py"考察的是字符串匹配和回溯算法。Python的字符串操作和递归可以构建出复杂的正则表达式匹配算法,帮助解决复杂的文本处理问题。 "palindrome-pairs.py"是关于回文串和字符串匹配的...
(/problems/regular-expression-matching/) 24.1% 难253 38.9% 中15 21.6% 中277 寻找名人 (/problems/find-the-celebrity/) 35.4% 中等158 读取 N 个字符给定 Read4 II - 多次调用 (/problems/read-n-...