在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a string containing digits from A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example: Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although the above answer is in lexicographical order, your answer could be in any order you want. 给定一个仅包含数字 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 示例: 输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 说明: 8ms 1 class Solution { 2 let strings:[String] = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"] 3 func letterCombinations(_ digits: String) -> [String] { 4 var res:[String] = [String]() 5 if digits.isEmpty {return res} 6 //回溯法 7 backtracking("",digits,0, &res) 8 return res 9 } 10 func backtracking(_ s:String ,_ digits:String,_ flag:Int,_ res:inout[String]) 11 { 12 13 //如果已经遍历完所有输入的数字,说明已经到达底部,需要向上溯源。 14 if flag >= digits.count 15 { 16 //到达底部说明已经是完整的一个结果,则将此次结果添加到结果集中 17 res.append(s); 18 //返回,向上回溯 19 return; 20 } 21 //0的ASCII:48 22 var chars:String = strings[digits.toInt(flag) - 48] 23 for char in chars.characters 24 { 25 var str:String = s + String(char) 26 backtracking(str,digits,flag+1,&res) 27 } 28 } 29 } 30 31 extension String { 32 //获取指定索引位置的字符,返回为字符串形式 33 func charAt(_ num:Int) -> String 34 { 35 guard num < self.count else { 36 assertionFailure("index out of range!") 37 return String() 38 } 39 let index = self.index(self.startIndex,offsetBy: num) 40 return String(self[index]) 41 } 42 43 //获取指定索引位置字符的ASCII整数值 44 func toInt(_ num:Int) -> Int 45 { 46 guard num < self.count else { 47 assertionFailure("index out of range!") 48 return 0 49 } 50 var number:Int = Int() 51 for scalar in charAt(num).unicodeScalars 52 { 53 number = Int(scalar.value) 54 } 55 return number 56 } 57 } 8ms 1 class Solution { 2 func letterCombinations(_ digits: String) -> [String] { 3 let dict:[Character: [String]] = 4 [ 5 "2": ["a", "b", "c"], 6 "3": ["d", "e", "f"], 7 "4": ["g", "h", "i"], 8 "5": ["j", "k", "l"], 9 "6": ["m", "n", "o"], 10 "7": ["p", "q", "r", "s"], 11 "8": ["t", "u", "v"], 12 "9": ["w", "x", "y", "z"], 13 14 ] 15 if digits.isEmpty{ 16 return [String]() 17 } 18 if digits.count == 1{ 19 return dict[Character(digits)]! 20 } 21 var nextComb = letterCombinations(String(digits.dropFirst())) 22 var current = dict[digits[digits.startIndex]]! 23 var result = [String]() 24 for c in current{ 25 for i in 0..<nextComb.count{ 26 result.append(String(c) + nextComb[i]) 27 } 28 } 29 return result 30 } 31 } 12ms 1 class Solution { 2 func letterCombinations(_ digits: String) -> [String] { 3 if digits.isEmpty == true { 4 return [] 5 } 6 7 var result: [String] = [] 8 // let chars = digits.characters 9 let hash: [String: [String]] = ["2": ["a", "b", "c"], 10 "3": ["d", "e", "f"], 11 "4": ["g", "h", "i"], 12 "5": ["j", "k", "l"], 13 "6": ["m", "n", "o"], 14 "7": ["p", "q", "r", "s"], 15 "8": ["t", "u", "v"], 16 "9": ["w", "x", "y", "z"]] 17 18 letterCombinationsHelper(digits, hash, &result, "") 19 20 return result 21 } 22 23 func letterCombinationsHelper(_ chars: String, 24 _ hash: [String: [String]], 25 _ result: inout [String], _ current: String) { 26 let count = chars.count 27 if count == 0 { 28 result.append(current) 29 return 30 } 31 32 let sub = String(chars[chars.index(chars.startIndex, offsetBy: 0)]) 33 let newList = hash[sub] ?? [] 34 for item in newList { 35 if count >= 1 { 36 let suubbb = String(chars[chars.index(chars.startIndex, offsetBy: 1)...]) 37 letterCombinationsHelper(suubbb, hash, &result, current + item) 38 } else { 39 letterCombinationsHelper("", hash, &result, current + item) 40 } 41 42 } 43 } 44 } 16ms 1 class Solution { 2 func letterCombinations(_ digits: String) -> [String] { 3 var combinations = [String](), combination = "" 4 dfs(creatBoard(), &combinations, &combination, Array(digits), 0) 5 return combinations 6 } 7 8 fileprivate func dfs(_ board: [String], _ combinations: inout [String], _ combination: inout String, _ digits: [Character], _ index: Int) { 9 if digits.count == index { 10 if combination != "" { 11 combinations.append(String(combination)) 12 } 13 14 return 15 } 16 17 let digitStr = board[Int(String(digits[index]))!] 18 for digitChar in digitStr { 19 combination.append(digitChar) 20 dfs(board, &combinations, &combination, digits, index + 1) 21 combination.removeLast() 22 } 23 } 24 25 26 fileprivate func creatBoard() -> [String] { 27 var res = [String]() 28 29 res.append("") 30 res.append("") 31 res.append("abc") 32 res.append("def") 33 res.append("ghi") 34 res.append("jkl") 35 res.append("mno") 36 res.append("pqrs") 37 res.append("tuv") 38 res.append("wxyz") 39 40 return res 41 } 42 } 16ms 1 class Solution { 2 private let keys = [" ", "*", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"] 3 4 func letterCombinations(_ digits: String) -> [String] { 5 6 if digits.count == 0 { 7 return [] 8 } 9 10 var answer = [String]() 11 12 let number = Array(digits) 13 14 addCombinations(number, 0, &answer, "") 15 16 return answer 17 } 18 19 20 func addCombinations(_ number: [Character], _ n: Int, _ answer: inout [String], _ char: String) { 21 22 //base case when we reach the end of the key number letters 23 if n >= number.count { 24 //add this to the answe 25 answer.append(char) 26 return 27 } 28 29 let digit = Int(String(number[n])) 30 31 let key = keys[digit!] 32 33 for k in key { 34 35 addCombinations(number, n + 1, &answer, char + String(k)) 36 } 37 } 38 } 20ms 1 class Solution { 2 func letterCombinations(_ digits: String) -> [String] { 3 4 if digits.count == 0 { 5 return [] 6 } 7 let dic:[Character:String] = ["2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"] 8 var res = [String]() 9 var path = [Character]() 10 let digits = Array(digits) 11 dfs(&res,&path,dic,digits,0) 12 13 return res 14 } 15 16 func dfs(_ res:inout [String], _ path: inout [Character], _ dic:[Character:String], _ digits: [Character], _ idx:Int) { 17 if path.count == digits.count { 18 res.append(String(path)) 19 return 20 } 21 22 if idx < digits.count { 23 if let letters = dic[digits[idx]] { 24 let chars = Array(letters) 25 26 for j in 0 ..< chars.count { 27 path.append(chars[j]) 28 dfs(&res,&path,dic,digits,idx+1) 29 path.removeLast() 30 } 31 } 32 } 33 } 34 } 20ms 1 class Solution {
2 func letterCombinations(_ digits: String) -> [String] {
3 var combinations = [String](), combination = ""
4
5 dfs(createBoard(), &combinations, &combination, Array(digits), 0)
6
7 return combinations
8 }
9
10 fileprivate func createBoard() -> [String] {
11 var res = [String]()
12
13 res.append("")
14 res.append("")
15 res.append("abc")
16 res.append("def")
17 res.append("ghi")
18 res.append("jkl")
19 res.append("mno")
20 res.append("pqrs")
21 res.append("tuv")
22 res.append("wxyz")
23
24 return res
25 }
26
27 fileprivate func dfs(_ board: [String], _ combinations: inout [String], _ combination: inout String, _ digits: [Character], _ index: Int) {
28 if digits.count == index {
29 if combination != "" {
30 combinations.append(String(combination))
31 }
32
33 return
34 }
35
36 let digitStr = board[Int(String(digits[index]))!]
37
38 for digitChar in digitStr {
39 combination.append(digitChar)
40 dfs(board, &combinations, &combination, digits, index + 1)
41 combination.removeLast()
42 }
43 }
44 }
24ms 1 class Solution { 2 func letterCombinations(_ digits: String) -> [String] { 3 var result = [String]() 4 let map:[Character:String] = ["2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"] 5 var arr = [String]() 6 for c in digits { 7 arr.append(map[c]!) 8 } 9 var count = 0 10 for ss in arr { 11 var resulta = [String]() 12 for c in ss{ 13 if count == 0{ 14 result.append(String(c)) 15 resulta.append(String(c)) 16 17 }else{ 18 for item in result{ 19 var i = item 20 i.append(c) 21 resulta.append(i) 22 } 23 24 } 25 } 26 result.removeAll() 27 result = resulta 28 count += 1 29 } 30 return result 31 } 32 } 28ms 1 class Solution { 2 3 var res = [String]() 4 5 let w = [[],[],["a","b","c"],["d","e","f"], 6 ["g","h","i"],["j","k","l"],["m",< |
请发表评论