用 leetcode 写的第一道题是 771 宝石与石头
这么写的
···java
public int numJewelsInStones(String J, String S) {
int count = 0;
char[] jstone = J.toCharArray();
char[] sstone = S.toCharArray();
for (char j : jstone) {
for (char s : sstone) {
if (s == j) {
count++;
}
}
}
return count;
}
···
能够执行通过,换成正则这么写
···java
char[] jstone = J.toCharArray();
for (char j : jstone) {
String regex = String .valueOf(j);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern .matcher(S);
while (matcher.find()) {
count++;
}
}
···
不能执行,返回错误 error: cannot find symbol: class Pattern
这么写的
···java
public int numJewelsInStones(String J, String S) {
int count = 0;
char[] jstone = J.toCharArray();
char[] sstone = S.toCharArray();
for (char j : jstone) {
for (char s : sstone) {
if (s == j) {
count++;
}
}
}
return count;
}
···
能够执行通过,换成正则这么写
···java
char[] jstone = J.toCharArray();
for (char j : jstone) {
String regex = String .valueOf(j);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern .matcher(S);
while (matcher.find()) {
count++;
}
}
···
不能执行,返回错误 error: cannot find symbol: class Pattern