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
这题是一个字符匹配题,但添加了一个“.”和“*”,注意*号是表示零个或多个前面的那个元素,然后这个可以用递归的思想,不过这题如果用c或c++做会简单点,指针比较灵活。
这个代码是从网上下载过来的,因为之前我理解错误了,把*号当成任意个相同的元素就行,真是作死啊。理解题目先啊
'.' 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
这题是一个字符匹配题,但添加了一个“.”和“*”,注意*号是表示零个或多个前面的那个元素,然后这个可以用递归的思想,不过这题如果用c或c++做会简单点,指针比较灵活。
public class Solution{ public boolean isMatch(String s, String p) { if(p.length() == 0) return s.length() == 0; if(p.length() == 1 || p.charAt(1) != '*'){ if(s.length() < 1 || (p.charAt(0) != '.' && s.charAt(0) != p.charAt(0))) return false; return isMatch(s.substring(1), p.substring(1)); }else{ int len = s.length(); int i = -1; while(i<len && (i < 0 || p.charAt(0) == '.' || p.charAt(0) == s.charAt(i))){ if(isMatch(s.substring(i+1), p.substring(2))) return true; i++; } return false; } }}
这个代码是从网上下载过来的,因为之前我理解错误了,把*号当成任意个相同的元素就行,真是作死啊。理解题目先啊
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 323Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 363Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 307Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 336Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 328Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 310Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 289Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 317Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 326Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 326Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 287Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 321Given n non-negative integers a ... -
Palindrome Number
2015-02-13 22:08 329Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 340Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 226Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 253The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 332Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 298You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 424[size=24px;]Longest Substring W ...
相关推荐
GNU Regex 程式库是 GNU 发展,提供操作比对 Regular Expression 文字字串的程式库,也就是使用 GNU Regex 程式库,可以作到以下的功能: 比对一字串是否完全与 Regular Expression 相幅合。 在一字串中寻找与 ...
Deep packet inspection (DPI) is widely used in content-aware network applications such as network intrusion detection systems,... Regular expression is the major tool for signature description due to its
采用NetFPGA平台进行深度包检测的实例,非常好的一个例子,相当详细的说明了处理过程。在DPI处理方面有着很好的指导意义。
Regular expression matching is widely used in content-aware applications, such as NIDS and protocol identification. However, wirespeed processing for large scale patterns still remains a great ...
Route and separate your data using regular expression matching Configure failover paths and load-balancing to remove single points of failure Utilize Gzip Compression for files written to HDFS ☆ ...
10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55...
performance, large-scale applications: improvements in the areas of buffer management, scalable network and file I/O, character-set support, and regular expression matching. Most of all, it boosts ...
正则表达式匹配也可以简单快速Regular Expression Matching Can Be Simple And Fast http://swtch.com/~rsc/regexp/
C#做的正则表达式匹配工具,可以实时显示匹配结果,带分组输出。 (C# do regular expression matching tool, you can display the matching results in real-time, with packet output.)
Unlike any other such tool, re2c focuses on generating high efficient code for regular expression matching. re2c is a preprocessor that generates C-based recognizers from regular expressions.
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/ ...
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 ...
It shows whether a regular expression matches a particular target string. It can also show which parts of the target string correspond to captured register groups or to arbitrary parts of the regular...
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 ...Expression Matching 011. Container With Most Water 012. Integer to Roman 013. Roman to Integer 014. Longest Common Prefix 019. R
Hound 的核心是基于这篇文章和代码的,作者是 Russ Cox:Regular Expression Matching with a Trigram Index。Hound 自身是个静态 React 前端,可以和 Go 后端进行交互,后端保持最新的,每个库的索引和通过极小的一...
Regular Expression Matching Wildcard Matching Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic ...
Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy ,...
the underlying character set support, and the powerful utilities for regular expression matching. <br>Chapter 14Threadsexplains the language's view of multithreading. Many applications, such as ...
10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone ...
分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。...Expression Matching 25.3% 困难 11 Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In