昨天才开始使用 leetcode,想问问 leetcode 上支持正则吗?

2018-11-23 15:56:49 +08:00
 backfrw
用 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
10217 次点击
所在节点    LeetCode
3 条回复
backfrw
2018-11-23 15:57:55 +08:00
另外,发帖怎么写代码片啊?
都是新手问题,谢谢大家
loshine1992
2018-11-23 16:16:20 +08:00
导入类啊。。
backfrw
2018-12-19 17:11:26 +08:00
@loshine1992 谢谢,以为 leetcode 就留了个方法入口,其他的会补全,才开始不会用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/510822

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX