在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word. Return the number of different transformations among all words we have. Example: Input: words = ["gin", "zen", "gig", "msg"] Output: 2 Explanation: The transformation of each word is: "gin" -> "--...-." "zen" -> "--...-." "gig" -> "--...--." "msg" -> "--...--." There are 2 different transformations, "--...-." and "--...--.". Note:
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: 为了方便,所有26个英文字母对应摩尔斯密码表如下: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。 返回我们可以获得所有词不同单词翻译的数量。 例如: 输入: words = ["gin", "zen", "gig", "msg"] 输出: 2 解释: 各单词翻译如下: "gin" -> "--...-." "zen" -> "--...-." "gig" -> "--...--." "msg" -> "--...--." 共有 2 种不同翻译, "--...-." 和 "--...--.". 注意:
Runtime: 16 ms
Memory Usage: 20.2 MB
1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 let array = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 4 return Set<String>(words.map { (word) -> String in 5 let aWord = word.uppercased() 6 var result = "" 7 aWord.unicodeScalars.forEach({ (s) in 8 result += array[Int(s.value) - 65] 9 }) 10 return result 11 }).count 12 } 13 } Runtime: 16 ms
Memory Usage: 19.9 MB
1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 var morse:[String] = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 4 var s:Set<String> = Set<String>() 5 for word in words 6 { 7 var t:String = String() 8 for c in word 9 { 10 t += morse[c.ascii - 97] 11 } 12 s.insert(t) 13 } 14 return s.count 15 } 16 } 17 18 //Character扩展 19 extension Character 20 { 21 //Character转ASCII整数值(定义小写为整数值) 22 var ascii: Int { 23 get { 24 return Int(self.unicodeScalars.first?.value ?? 0) 25 } 26 } 27 } 24ms 1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 func transform(_ position: UInt32) -> String? { 4 let morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 5 let offset = "a".unicodeScalars.first!.value 6 return morse[Int(position - offset)] 7 } 8 var result = Set<String>() 9 for word in words { 10 let morseCode = word.unicodeScalars.compactMap { transform($0.value) }.joined() 11 result.update(with: morseCode) 12 } 13 return result.count 14 } 15 } 24ms 1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 var morse : [String] = Array(repeating: "", count: 97) 4 morse.append(contentsOf: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]) 5 6 var wordsSet : Set<String> = [] 7 var morseWord : String = "'" 8 for word in words { 9 morseWord = "'" 10 for w in word.unicodeScalars { 11 morseWord.append(contentsOf: morse[Int(w.value)]) 12 } 13 wordsSet.update(with: morseWord) 14 } 15 return wordsSet.count 16 } 17 } 40ms 1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 4 let morseCodeArray = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 5 let alphabets = "abcdefghijklmnopqrstuvwxyz" 6 7 var morseSet: Set<String> = [] 8 9 for word in words { 10 var morseWord: String = "" 11 12 for character in word { 13 14 var order = 0 15 var counter = 0 16 17 for alphabet in alphabets { 18 19 if character == alphabet { 20 counter = order 21 } 22 order += 1 23 } 24 morseWord += morseCodeArray[counter] 25 } 26 morseSet.insert(morseWord) 27 } 28 return morseSet.count 29 } 30 } 36ms 1 class Solution { 2 private let passwordTable: [Character : String] = [ 3 "a" : ".-", 4 "b" : "-...", 5 "c" : "-.-.", 6 "d" : "-..", 7 "e" : ".", 8 "f" : "..-.", 9 "g" : "--.", 10 "h" : "....", 11 "i" : "..", 12 "j" : ".---", 13 "k" : "-.-", 14 "l" : ".-..", 15 "m" : "--", 16 "n" : "-.", 17 "o" : "---", 18 "p" : ".--.", 19 "q" : "--.-", 20 "r" : ".-.", 21 "s" : "...", 22 "t" : "-", 23 "u" : "..-", 24 "v" : "...-", 25 "w" : ".--", 26 "x" : "-..-", 27 "y" : "-.--", 28 "z" : "--..", 29 ] 30 31 func uniqueMorseRepresentations(_ words: [String]) -> Int { 32 return Set(words.map { 33 $0.map({ 34 passwordTable[$0]! 35 }).reduce("", { 36 $0 + $1 37 }) 38 }).count 39 } 40 } 40ms 1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 // 字母表莫尔斯密码字典 4 var alphabet = Dictionary<String,String> () 5 let charAry = Array("abcdefghijklmnopqrstuvwxyz") 6 let morseAry = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 7 var res = Array<String>() 8 var resList = Array<String>() 9 10 for (index,value) in charAry.enumerated() { 11 // String(value):char转string 映射字母表莫尔斯密码成字典 12 alphabet[String(value)] = morseAry[index] 13 } 14 // 遍历 ["gin", "zen", "gig", "msg"] 15 for (_,value) in words.enumerated() { 16 var tmp = "" 17 // 遍历 "gin" 18 for (_,value1) in String(value).enumerated() { 19 tmp.append(alphabet[String(value1)]!) 20 } 21 res.append(tmp) // "gin" => "--...-." 22 } 23 // 去重 24 for (_,value) in res.enumerated() { 25 if !resList.contains(value) { 26 resList.append(value) 27 } 28 } 29 30 return resList.count 31 } 32 } 48ms 1 class Solution { 2 func uniqueMorseRepresentations(_ words: [String]) -> Int { 3 guard words.count > 1 else { 4 return words.count 5 } 6 7 var transformationMap:[String: Bool] = [String: Bool]() 8 var moorseDict:[Character: String] = 9 ["a": ".-","b": "-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-", 全部评论
专题导读
上一篇:TensorFlow开发者会峰会:支持Swift,更好的支持JavaScript发布时间:2022-07-13下一篇:[Swift]LeetCode215.数组中的第K个最大元素|KthLargestElementinanArray发布时间:2022-07-13热门推荐
热门话题
阅读排行榜
|
请发表评论