LeetCode【771. 宝石与石头】

这个也是使用双for,判断是否两字符串相等,若相等count就加1.

class Solution {
    public int numJewelsInStones(String J, String S) {
        int c1 = J.length();
        int c2 = S.length();
        int m,n;
        int count = 0;
        for(m = 0;m <= c2-1;m++)
        {
            for(n = 0;n <= c1-1;n++)
            {
                String t = J.substring(n,n+1);
                String s1 = S.substring(m, m+1);
                if(s1.equals(t))
                {
                    count++;
                }
            }
        }
        return count;
    }
}

注意两字符串相等必须使用xx.equals(xx),不能用==

猜你喜欢

转载自www.cnblogs.com/wzwi/p/10929192.html