Files
LeetCode-Go/leetcode/0771.Jewels-and-Stones/771. Jewels and Stones.go
2020-08-07 17:06:53 +08:00

29 lines
442 B
Go

package leetcode
import "strings"
// 解法一
func numJewelsInStones(J string, S string) int {
count := 0
for i := range S {
if strings.Contains(J, string(S[i])) {
count++
}
}
return count
}
// 解法二
func numJewelsInStones1(J string, S string) int {
cache, result := make(map[rune]bool), 0
for _, r := range J {
cache[r] = true
}
for _, r := range S {
if _, ok := cache[r]; ok {
result++
}
}
return result
}