[Swift]LeetCode771. 寶石與石頭 | Jewels and Stones

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-hdhjfyas-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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".github

Example 1:微信

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

Example 2:ide

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

Note:spa

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

 給定字符串J 表明石頭中寶石的類型,和字符串 S表明你擁有的石頭。 S 中每一個字符表明了一種你擁有的石頭的類型,你想知道你擁有的石頭中有多少是寶石。code

J 中的字母不重複,J 和 S中的全部字符都是字母。字母區分大小寫,所以"a""A"是不一樣類型的石頭。htm

示例 1:blog

輸入: 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 }
相關文章
相關標籤/搜索