LeetCode 771. 宝石与石头

https://leetcode-cn.com/problems/jewels-and-stones/

 

Java

public int numJewelsInStones(String J, String S) {
        if (null == J || null == S || J.isEmpty() || S.isEmpty()) {
            return 0;
        }
        char[] jChars = J.toCharArray();
        // ASCII字母为 65~122
        byte[] letterArray = new byte[58];
        for (char jChar : jChars) {
            letterArray[jChar - 65] = 1;
        }
        char[] sChars = S.toCharArray();
        //  宝石总数
        int count = 0;
        for (char sChar : sChars) {
            if (letterArray[sChar - 65] == 1) {
                count++;
            }
        }
        return count;
    }

  

 

posted @ 2020-10-02 16:13  Risfeng  阅读(133)  评论(0编辑  收藏  举报