V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
backfrw
V2EX  ›  LeetCode

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

  •  
  •   backfrw · 2018-11-23 15:56:49 +08:00 · 10171 次点击
    这是一个创建于 1952 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 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
    3 条回复    2018-12-19 17:11:26 +08:00
    backfrw
        1
    backfrw  
    OP
       2018-11-23 15:57:55 +08:00
    另外,发帖怎么写代码片啊?
    都是新手问题,谢谢大家
    loshine1992
        2
    loshine1992  
       2018-11-23 16:16:20 +08:00
    导入类啊。。
    backfrw
        3
    backfrw  
    OP
       2018-12-19 17:11:26 +08:00
    @loshine1992 谢谢,以为 leetcode 就留了个方法入口,其他的会补全,才开始不会用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1666 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:46 · PVG 00:46 · LAX 09:46 · JFK 12:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.