【JAVA、C++】LeetCode 010 Regular Expression Matching
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
这道题初学者或多或少都参考了网上的答案,主要难点有以下
一、要理解正则表达式中.和*的含义,“.”代表任意字符,但是“a*”代表“”,“a”,“aa”,“aaa”……这一点比较容易让人犯迷糊
二、必须完全比配,即isMatch("aa","aaa") → false
三、第一个参数String s 是不含有“.”和“*”,因此不要把程序复杂化
Java实现如下:
static public boolean isMatch(String s, String p) {
// 如果从s长度入手,s.length() == 0时("","a*")、("","a*b*")都会返回true
if (p.length() == 0)
return s.length() == 0;
else if (p.length() == 1)
return s.length() == 1&& (p.charAt(0) == '.' || s.charAt(0) == p.charAt(0));
if (p.charAt(1) != '*') {
if (s.length() == 0|| (p.charAt(0) != '.' && s.charAt(0) != p.charAt(0)))
return false;
return isMatch(s.substring(1), p.substring(1));
}
else if (isMatch(s, p.substring(2)))
return true;
else {
int i = 0;
while (i < s.length()&& (p.charAt(0) == '.' || p.charAt(0) == s.charAt(i))) {
if (isMatch(s.substring(i + 1), p.substring(2)))
return true;
i++;
}
}
return false;
}
C++面向对象:
class Solution {
public:
bool isMatch(string s, string p) {
if (p.length() == )
return s.length() == ;
else if (p.length() == )
return s.length() == && (p[] == '.' || s[] == p[]);
if (p[] != '*') {
if (s.length() == || (p[] != '.' && s[] != p[]))
return false;
return isMatch(s.substr(), p.substr());
}
else if (isMatch(s, p.substr()))
return true;
else {
int i = ;
while (i < s.length() && (p[] == '.' || p[] == s[i])) {
if (isMatch(s.substr(i + ), p.substr()))
return true;
i++;
}
}
return false;
}
};
C++ 面向过程(效率高):
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (!p[])
return !s[];
if (!p[] || p[] != '*')
return s[] && (p[] == '.' || s[] == p[])&& isMatch(++s, ++p);
while (s[] && (p[] == '.' || s[] == p[]))
if (isMatch(s++, p + ))
return true;
return isMatch(s, p + );
}
bool isMatch(string s, string p) {
return isMatch(s.data(), p.data());
}
};
【JAVA、C++】LeetCode 010 Regular Expression Matching的更多相关文章
- LeetCode 010 Regular Expression Matching
题目描述:Regular Expression Matching Implement regular expression matching with support for '.' and '*' ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- hihocoder #1270 建造基地
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...
- android 开发问题:java.lang.ClassCastException
java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl ...
- Xcode7中新技术
Xcode7真加了两个重要的debug功能:1:Address Sanitizer: 再也不用担心 EXC_BAD_ACCESS 在项目的Scheme中Diagnostics下,选中enable ad ...
- IOS基础之 (十二) 类的扩展
对OC类的扩展总结如下,共有4个: 1.子类 subClass 作用:可以使用类的继承来增添父类的变量和方法. 写法:在.h文件中 @interface Student : Person 2.分类 C ...
- ios 关联对象运用 objc_setAssociatedObject
点按钮的时候,给alertView添加一个关联对象(被点击这个按钮), objc_setAssociatedObject(alert, &kRepresentedObject, sender, ...
- WebSocket 基本函数
1.构造函数 WebSocket(char *host); 创建一个websocket对象,接受一个参数以ws://靠头,就像发起一个HTTP请求一样用http://开头 var ws=new W ...
- DEDECMS 后台登录空白怎么办 后台无法登陆
刚安装完dedecms,兴致冲冲的准备进后台,输入完用户名和密码后,页面 中显示一片空白. 立马到网上搜搜,发现大家各抒己见,但是都没有解决问题. 不过,下面的这个方法是可以的.马上记录下来,以备其他 ...
- C语言异常处理和连接数据库
#include <stdio.h> #include <setjmp.h> jmp_buf j; void Exception(void); double diva(doub ...
- C#常用函数--通用篇
C#常用函数→通用篇转载地址→http://www.cnblogs.com/superfang/archive/2008/07/02/1233706.html以前我都是"原文地址" ...
- iOS 用instancetype代替id作返回类型有什么好处?
2014-07-07更新:苹果在iOS 8中全面使用instancetype代替id Steven Fisher:只要一个类返回自身的实例,用instancetype就有好处. @interface ...