classSolution{ publicintnumJewelsInStones(String J, String S){ int res = 0; HashMap<Character, Integer> map = new HashMap<>(); for (char c: J.toCharArray()) { if (!map.containsKey(c)) { map.put(c, 1); } } for (char c: S.toCharArray()) { if (map.containsKey(c)) res++; } return res; } }
两层循环遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution{ publicintnumJewelsInStones(String J, String S){ int res = 0; for (char s: S.toCharArray()) { for (char c: J.toCharArray()) { if (s == c) { res++; break; } } } return res; } }