leetcode1:宝石和石头

leetcode1:宝石和石头
解法1:双层遍历,比较每个字符的值

class Solution {
    public int numJewelsInStones(String J, String S) {
        int Jlength = J.length();
        int Slength = S.length();
        
        int count = 0;
        
        for(int i = 0; i<Jlength;i++) {
         for(int j = 0;j<Slength;j++) {
          if(J.charAt(i)==S.charAt(j))
           count++;
         }
        }
        return count;
    }
}

解法2:用正则方式,将J进行替换,计算替换的字符数

class Solution {
    public int numJewelsInStones(String J, String S) {
        return S.replaceAll("[^" + J + "]", "").length();
    }
}

解法3:利用hashSet的contains方法,只需遍历S字符数组即可
leetcode1:宝石和石头

相关文章:

  • 2021-08-12
  • 2021-04-26
  • 2021-10-29
  • 2021-10-18
  • 2022-03-05
  • 2022-12-23
  • 2021-09-29
  • 2021-06-14
猜你喜欢
  • 2021-06-15
  • 2021-10-14
  • 2021-05-04
  • 2021-07-19
  • 2021-10-01
  • 2021-06-22
  • 2021-07-08
相关资源
相似解决方案