敢教日月换新天。为有牺牲多壮志,

[Swift]LeetCode771. 宝石与石头 | Jewels and Stones

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10536345.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example 1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

Input: J = "z", S = "ZZ"
Output: 0

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a""A"是不同类型的石头。

示例 1:

输入: J = "aA", S = "aAAbbbb"
输出: 3

示例 2:

输入: J = "z", S = "ZZ"
输出: 0

注意:

  • S 和 J 最多含有50个字母。
  •  J 中的字符不重复。

Runtime: 8 ms
Memory Usage: 19.3 MB
复制代码
 1 class Solution {
 2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
 3         var jewels = [Character : Int]()
 4         J.forEach { (c) in
 5             jewels[c] = 1
 6         }
 7 
 8         return S.reduce(0, { result, c in
 9             if let _ = jewels[c] {
10                 return result + 1
11             }
12             return result
13         })
14     }
15 }
复制代码

8ms

复制代码
 1 class Solution {
 2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
 3         var count: Int = 0
 4         for character in S {
 5             if J.contains(character) {
 6                 count += 1
 7             }
 8         }
 9         return count
10     }
11 }
复制代码

12ms

1 class Solution {
2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
3         return S.characters.filter{
4             J.characters.contains($0)
5         }.count
6     }
7 }

16ms

复制代码
 1 class Solution {
 2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
 3         var res = 0
 4         for x in J{
 5             for y in S{
 6                 if(y==x){
 7                     res+=1
 8                 }
 9             } 
10         }  
11         return res
12     }
13 }
复制代码

20ms

复制代码
 1 class Solution {
 2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
 3         var matchArray: [Character] = []
 4         var stoneArray = Array(S)
 5         
 6         for jewel in J {
 7             if stoneArray.contains(jewel) {
 8                 matchArray += stoneArray.filter { $0 == jewel }
 9             }
10         }
11         
12         return matchArray.count
13     }
14 }
复制代码

19064 kb

复制代码
 1 class Solution {
 2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
 3     var num = 0
 4     J.forEach { (Jcharacter) in
 5         S.forEach({ (Scharacter) in
 6             if Jcharacter == Scharacter {
 7                 num += 1
 8             }
 9         })
10     }
11     
12     return num
13     }
14 }
复制代码

48ms

复制代码
 1 class Solution {
 2     func numJewelsInStones(_ J: String, _ S: String) -> Int {
 3         var map: [Character:Int?] = [:]
 4         for str in J {
 5             map[str] = 0
 6         }
 7         for str in S {
 8             if let value = map[str] {
 9                 map[str] = (value ?? 0) + 1
10             }
11         }
12         return map.values.reduce(0) {$0 + ($1 ?? 0)}
13     }
14 }
复制代码

 

posted @   为敢技术  阅读(308)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°