由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道leetcode变种,twitter常考,怎么做?
相关主题
java没有指针真麻烦C++ Q66: reverse a string -- is it efficient
regex 用DP做对不对啊?C++ 面试题
面试常考哪些java的design pattern问一道C++编程题
贴一个OJ 的 longest valid parenthesisGoogle 2 phone interviews exposed + 求祝福
请教:string pattern match 题Microsoft interview question
问道题string pattern match的题目请教一个C++问题
C++ Q23: if if elseString Pattern Matching problems
leetcode ValidNumber问题的代码,供参考懒得写了,想练手的就写写贴在这里吧
相关话题的讨论汇总
话题: match话题: return话题: aab话题: else话题: char
进入JobHunting版参与讨论
1 (共1页)
S*******C
发帖数: 822
1
pattern match的题
// Text: a-z, A-Z, 0-9
// Pattern: a-z, A-z, 0-9, +, *
// + = 1 or more times
// * = 0 or more times. [email protected]/* */ 3 acres
//
// Pattern: a+b
// Text: aab, b return true-google 1point3acres
//
// Pattern: a+b*
// Text: aab, aa return true
I**********s
发帖数: 441
2
a+b应该不match b吧?
代码如下:
bool match(const char * s, const char * p) {
if (! *p) return ! *s;
if (*(p + 1) == '*') {
// match(s+1, p) - match next char in s.
// match(s, p+2) - match exactly nothing in s.
if (*p == *s) return match(s+1, p) || match(s, p+2);
else return match(s, p+2); // matche exactly nothing in s.
}
else if (*(p + 1) == '+') {
// match(s+1, p) - match next char in s.
// match(s+1, p+2) - match exactly 1 char in s.
if (*p == *s) return match(s+1, p) || match(s+1, p+2);
else return false;
}
else if (*p == *s) {
return match(s+1, p+1); // match exactly 1 char in s.
}
else {
return false;
}
}

【在 S*******C 的大作中提到】
: pattern match的题
: // Text: a-z, A-Z, 0-9
: // Pattern: a-z, A-z, 0-9, +, *
: // + = 1 or more times
: // * = 0 or more times. [email protected]/* */ 3 acres
: //
: // Pattern: a+b
: // Text: aab, b return true-google 1point3acres
: //
: // Pattern: a+b*

S*******C
发帖数: 822
3
a+b不match b,但很多test case通不过,看看怎么回事
public class Solution {
public boolean match(String s, String p) {
return match(s, 0, p, 0);
}
private boolean match(String s, int i, String p, int j) {
if (j >= p.length() - 1) {
return i >= s.length() - 1;
} else if (i >= s.length() - 1) {
return j >= p.length() - 1;
}
if (p.charAt(j + 1) == '*') {
// match(s+1, p) - match next char in s.
// match(s, p+2) - match exactly nothing in s.
if (s.charAt(i) == p.charAt(j))
return match(s, i + 1, p, j) || match(s, i, p, j + 2);
else
return match(s, i, p, j + 2); // matches exactly nothing in
s.
} else if (p.charAt(j + 1) == '+') {
// match(s+1, p) - match next char in s.
// match(s+1, p+2) - match exactly 1 char in s.
if (s.charAt(i) == p.charAt(j))
return match(s, i + 1, p, j) || match(s, i + 1, p, j + 2);
else
return false;
} else if (s.charAt(i) == p.charAt(j)) {
return match(s, i + 1, p, j + 1); // match exactly 1 char in s.
} else {
return false;
}
}
public static void main(String args[]) {
test("aab", "a+b");//true
test("aab", "aab");//true
test("aab", "aa*b");//true
test("aab", "a+b*");//true
test("aab", "aa+b");//true
test("aab", "a+b+");//true
test("aab", "a+b*");//true
test("aab", "aaa+b");//false
test("aab", "a+bb+");//false
}
static void test(String s, String p) {
System.out.println(sol.match(s, p));
}
static Solution sol = new Solution();
}

【在 I**********s 的大作中提到】
: a+b应该不match b吧?
: 代码如下:
: bool match(const char * s, const char * p) {
: if (! *p) return ! *s;
: if (*(p + 1) == '*') {
: // match(s+1, p) - match next char in s.
: // match(s, p+2) - match exactly nothing in s.
: if (*p == *s) return match(s+1, p) || match(s, p+2);
: else return match(s, p+2); // matche exactly nothing in s.
: }

I**********s
发帖数: 441
4
这涉及到C++转换成Java的一点小技巧。这样改就可以都通过了:
private boolean match(String s, int i, String p, int j) {
if (j == p.length()) return i == s.length();
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
// match(s+1, p) - match next char in s.
// match(s, p+2) - match exactly nothing in s.
if (i < s.length() && s.charAt(i) == p.charAt(j))
return match(s, i + 1, p, j) || match(s, i, p, j + 2);
else
return match(s, i, p, j + 2); // matches exactly nothing in
s.
} else if (j + 1 < p.length() && p.charAt(j + 1) == '+') {
// match(s+1, p) - match next char in s.
// match(s+1, p+2) - match exactly 1 char in s.
if (i < s.length() && s.charAt(i) == p.charAt(j))
return match(s, i + 1, p, j) || match(s, i + 1, p, j + 2);
else
return false;
} else if (i < s.length() && s.charAt(i) == p.charAt(j)) {
return match(s, i + 1, p, j + 1); // match exactly 1 char in s.
} else {
return false;
}
}
S*******C
发帖数: 822
5
谢谢,你真牛啊

in

【在 I**********s 的大作中提到】
: 这涉及到C++转换成Java的一点小技巧。这样改就可以都通过了:
: private boolean match(String s, int i, String p, int j) {
: if (j == p.length()) return i == s.length();
: if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
: // match(s+1, p) - match next char in s.
: // match(s, p+2) - match exactly nothing in s.
: if (i < s.length() && s.charAt(i) == p.charAt(j))
: return match(s, i + 1, p, j) || match(s, i, p, j + 2);
: else
: return match(s, i, p, j + 2); // matches exactly nothing in

I**********s
发帖数: 441
6
哈哈哈,不用客气。
1 (共1页)
进入JobHunting版参与讨论
相关主题
懒得写了,想练手的就写写贴在这里吧请教:string pattern match 题
求STRING COMPRESSION一题C++解法(CC150 1.5)问道题string pattern match的题目
从Simplify Path问面试编程语言选择?C++ Q23: if if else
问一个C++ set和unordered_set iterator的问题leetcode ValidNumber问题的代码,供参考
java没有指针真麻烦C++ Q66: reverse a string -- is it efficient
regex 用DP做对不对啊?C++ 面试题
面试常考哪些java的design pattern问一道C++编程题
贴一个OJ 的 longest valid parenthesisGoogle 2 phone interviews exposed + 求祝福
相关话题的讨论汇总
话题: match话题: return话题: aab话题: else话题: char