• [Swift]LeetCode49. 字母异位词分组 | Group Anagrams


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

    Given an array of strings, group anagrams together.

    Example:

    Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
    Output:
    [
      ["ate","eat","tea"],
      ["nat","tan"],
      ["bat"]
    ]

    Note:

    • All inputs will be in lowercase.
    • The order of your output does not matter.

    给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

    示例:

    输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
    输出:
    [
      ["ate","eat","tea"],
      ["nat","tan"],
      ["bat"]
    ]

    说明:

    • 所有输入均为小写字母。
    • 不考虑答案输出的顺序。

    68ms

     1 class Solution {
     2     func hashValue(_ str: String) -> UInt64 {
     3         var hash: UInt64 = 0
     4         for code in str.unicodeScalars {
     5             let value = code.value - 97
     6             hash += UInt64(pow(5.0, Double(value)))
     7         }
     8 
     9         return hash
    10     }
    11 
    12     func groupAnagrams(_ strs: [String]) -> [[String]] {
    13         var dic = [UInt64 : [String]]()
    14         for str in strs {
    15             let hash = hashValue(str)
    16             if let strings = dic[hash] {
    17                 dic[hash] = strings + [str]
    18             } else {
    19                 dic[hash] = [str]
    20             }
    21         }
    22         return Array(dic.values)
    23     }
    24 }

    124ms

     1 class Solution {
     2     func groupAnagrams(_ strs: [String]) -> [[String]] {
     3         var result = [String : [String]]()
     4         for item in strs {
     5             let newStr = reformateString(item)
     6             if let tempArray = result[newStr] {
     7                 var newArray = tempArray
     8                 newArray.append(item)
     9                 result[newStr] = newArray
    10             } else {
    11                 result[newStr] = [item]
    12             }
    13         }
    14         return [[String]](result.values)
    15     }
    16     
    17     func reformateString(_ str: String) -> String {
    18        return String([Character](str).sorted(by: <))
    19     }
    20 }

    132ms

     1 class Solution {
     2     func groupAnagrams(_ strs: [String]) -> [[String]] {
     3         var res = [[String]]()
     4         var dict = [String: [String]]()
     5         strs.forEach{(string) in
     6             let charArray = Array(string).sorted(){$0 < $1}
     7             let key = String(charArray)
     8             if dict[key] != nil {
     9                 dict[key]?.append(string)
    10             }else {
    11                 dict[key] = [string]
    12             }
    13         }
    14         for key in dict.keys {
    15             res.append(dict[key]!)
    16         }
    17         return res
    18     }
    19 }

    140ms

     1 class Solution {
     2     func groupAnagrams(_ strs: [String]) -> [[String]] {
     3         var map = [String: [String]]()
     4         
     5         for str in strs {
     6             let key = String(str.sorted())
     7             map[key, default: []].append(str)
     8         }
     9         
    10         return Array(map.values)
    11     }
    12 }

    148ms

     1 class Solution {
     2     func groupAnagrams(_ strs: [String]) -> [[String]] {
     3         var map: [String: [String]] = [:]
     4         for str in strs {
     5             let chars = str.sorted()
     6             let key = String(chars)
     7             if var list = map[key] {
     8                 list.append(str)
     9                 map[key] = list
    10             } else {
    11                 map[key] = [str]
    12             }
    13         }
    14         return Array(map.values)
    15     }
    16 }

    316ms

     1 class Solution {
     2     func groupAnagrams(_ strs: [String]) -> [[String]] {
     3         var groups = [String: [String]]()
     4         for str in strs {
     5             let strSorted = String(str.sorted())
     6             if groups[strSorted] == nil {
     7                 groups[strSorted] = [str]
     8             } else {
     9                 groups[strSorted]?.append(str)
    10             }
    11         }
    12         return groups.map { $1 }
    13     }
    14 }
  • 相关阅读:
    每天一个topic----route 设置
    每周一个topic IGMP -- 2013.08.13
    HTTP API 加签名规则
    gitbook 编辑手册
    phpMyAdmin 配置文档
    扒房源线索消息推送设计
    jetty 必知必会
    回溯法解决八皇后问题
    求m行n列个方格的正方形总数
    IOC和AOP的简单实现
  • 原文地址:https://www.cnblogs.com/strengthen/p/9907946.html
Copyright © 2020-2023  润新知