偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

LeetCode之電話號(hào)碼的字母組合

開(kāi)發(fā) 前端
給定一個(gè)僅包含數(shù)字 2-9 的字符串,返回所有它能表示的字母組合。答案可以按 任意順序 返回。給出數(shù)字到字母的映射如下(與電話按鍵相同)。注意 1 不對(duì)應(yīng)任何字母。

[[442756]]

前言

我們社區(qū)陸續(xù)會(huì)將顧毅(Netflix 增長(zhǎng)黑客,《iOS 面試之道》作者,ACE 職業(yè)健身教練。微博:@故胤道長(zhǎng)[1])的 Swift 算法題題解整理為文字版以方便大家學(xué)習(xí)與閱讀。

LeetCode 算法到目前我們已經(jīng)更新了 16 期,我們會(huì)保持更新時(shí)間和進(jìn)度(周一、周三、周五早上 9:00 發(fā)布),每期的內(nèi)容不多,我們希望大家可以在上班路上閱讀,長(zhǎng)久積累會(huì)有很大提升。

不積跬步,無(wú)以至千里;不積小流,無(wú)以成江海,Swift社區(qū) 伴你前行。如果大家有建議和意見(jiàn)歡迎在文末留言,我們會(huì)盡力滿足大家的需求。

難度水平:中等

1. 描述

給定一個(gè)僅包含數(shù)字 2-9 的字符串,返回所有它能表示的字母組合。答案可以按 任意順序 返回。

給出數(shù)字到字母的映射如下(與電話按鍵相同)。注意 1 不對(duì)應(yīng)任何字母。

[[442757]]

2. 示例

示例 1

  1. 輸入:digits = "23" 
  2. 輸出:["ad","ae","af","bd","be","bf","cd","ce","cf"

示例 2

  1. 輸入:digits = "" 
  2. 輸出:[] 

示例 3

  1. 輸入:digits = "2" 
  2. 輸出:["a","b","c"

約束條件:

  • 0 <= digits.length <= 4
  • digits[i] 是范圍 ['2', '9'] 的一個(gè)數(shù)字

3. 答案

  1. class LetterCombinationsPhoneNumber { 
  2.     func letterCombinations(_ digits: String) -> [String] { 
  3.         guard digits.count > 0 else { 
  4.             return [String]() 
  5.         } 
  6.          
  7.         var combinations = [String](), combination = "" 
  8.         let numberToStr = ["""""abc""def""ghi""jkl""mno""pqrs""tuv""wxyz"
  9.          
  10.         dfs(&combinations, &combination, numberToStr, digits, 0) 
  11.          
  12.         return combinations 
  13.     } 
  14.      
  15.     private func dfs(_ combinations: inout [String], _ combination: inout String, _ numberToStr: [String], _ digits: String, _ indexInt) { 
  16.         if combination.count == digits.count { 
  17.             combinations.append(combination) 
  18.             return 
  19.         } 
  20.          
  21.         let currentStr = fetchCurrentStr(from: digits, atindex, numberToStr) 
  22.          
  23.         for char in currentStr { 
  24.             combination.append(char
  25.             dfs(&combinations, &combination, numberToStr, digits, index + 1) 
  26.             combination.removeLast() 
  27.         } 
  28.     } 
  29.          
  30.     private func fetchCurrentStr(from digits: String, at indexInt, _ numberToStr: [String]) -> String { 
  31.         guard index >= 0 && index < digits.count else { 
  32.             fatalError("Invalid index"
  33.         } 
  34.          
  35.         let currentDigitChar = digits[digits.index(digits.startIndex, offsetBy: index)] 
  36.          
  37.         guard let currentDigit = Int(String(currentDigitChar)), currentDigit >= 0, currentDigit < numberToStr.count else { 
  38.             fatalError("Invalid digits"
  39.         } 
  40.          
  41.         return numberToStr[currentDigit] 
  42.     } 
  • 主要思想:經(jīng)典的深度優(yōu)先搜索,首先創(chuàng)建電話板
  • 時(shí)間復(fù)雜度:O(4^n), n 表示數(shù)字長(zhǎng)度
  • 空間復(fù)雜度:O(n), n 表示數(shù)字長(zhǎng)度

該算法題解的倉(cāng)庫(kù):LeetCode-Swift[2]

點(diǎn)擊前往 LeetCode[3] 練習(xí)

參考資料

[1]@故胤道長(zhǎng):

https://m.weibo.cn/u/1827884772

[2]LeetCode-Swift:

https://github.com/soapyigu/LeetCode-Swift[3]LeetCode: https://leetcode.com/problems/letter-combinations-of-a-phone-number

 

責(zé)任編輯:姜華 來(lái)源: Swift社區(qū)
相關(guān)推薦

2017-01-10 13:42:18

大數(shù)據(jù)深度學(xué)習(xí)識(shí)別圖片

2009-06-26 10:15:27

Google語(yǔ)音服務(wù)

2018-05-29 16:43:51

VDIIDV字母

2021-12-06 11:51:43

靜態(tài)庫(kù)動(dòng)態(tài)庫(kù)C語(yǔ)言

2020-03-11 08:52:17

Session開(kāi)源通信應(yīng)用

2013-04-10 18:12:57

2015-07-23 10:43:47

云端數(shù)據(jù)存儲(chǔ)PostgreSQL在SparkTG

2021-09-17 05:42:13

微信一證通查騰訊

2015-08-19 09:21:19

國(guó)際電話區(qū)號(hào)代碼實(shí)踐

2021-07-12 11:15:20

黑客數(shù)據(jù)泄露網(wǎng)絡(luò)攻擊

2015-05-08 09:57:59

綁定端口端口復(fù)用網(wǎng)絡(luò)編程

2025-02-28 08:00:00

Java數(shù)據(jù)類型JVM

2021-09-06 11:51:26

項(xiàng)目C語(yǔ)言開(kāi)發(fā)

2021-04-05 18:10:04

網(wǎng)絡(luò)安全數(shù)據(jù)泄露Facebook

2009-08-08 22:10:29

IP地址的分配局域網(wǎng)設(shè)置

2014-08-13 18:31:33

谷歌垃圾郵件欺詐郵件

2023-12-27 07:56:29

內(nèi)存哈希算法排序算法

2020-11-17 06:43:16

安卓智能手機(jī)移動(dòng)應(yīng)用

2024-10-10 14:11:51

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)