自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

LeetCode之最長(zhǎng)公共前綴

開(kāi)發(fā) 前端
編寫(xiě)一個(gè)函數(shù)來(lái)查找字符串?dāng)?shù)組中的最長(zhǎng)公共前綴。如果不存在公共前綴,返回空字符串 ""。

[[440913]]

前言

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

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

不積跬步,無(wú)以至千里;不積小流,無(wú)以成江海,Swift社區(qū) 伴你前行。

難度水平:簡(jiǎn)單

1. 描述

編寫(xiě)一個(gè)函數(shù)來(lái)查找字符串?dāng)?shù)組中的最長(zhǎng)公共前綴。

如果不存在公共前綴,返回空字符串 ""。

2. 示例

示例 1

  1. 輸入:strs = ["flower","flow","flight"
  2. 輸出:"fl" 

示例 2

  1. 輸入:strs = ["dog","racecar","car"
  2. 輸出:"" 
  3. 解釋?zhuān)狠斎氩淮嬖诠睬熬Y。 

約束條件:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] 僅由小寫(xiě)英文字母組成

3. 答案

  1. class LongestCommonPrefix { 
  2.     func longestCommonPrefix(_ strs: [String]) -> String { 
  3.         guard let firstStr = strs.first else { 
  4.             return "" 
  5.         } 
  6.          
  7.         var res = "" 
  8.          
  9.         for (i, charin firstStr.enumerated() { 
  10.             // dropFirst(_ k: Int = 1) returns a Substring struct 
  11.             for str in strs.dropFirst() { 
  12.                 if i == str.count { 
  13.                     return res 
  14.                 } 
  15.                  
  16.                 // Another easy way: Array(str)[i], time complexity is linear though 
  17.                 let currentStrChar = str[str.index(str.startIndex, offsetBy: i)] 
  18.                  
  19.                 if char != currentStrChar { 
  20.                     return res 
  21.                 } 
  22.             } 
  23.             res.append(char
  24.         } 
  25.          
  26.         return res 
  27.     } 
  • 主要思想:首先使用第一個(gè)字符串作為結(jié)果,在迭代數(shù)組時(shí)修改
  • 時(shí)間復(fù)雜度: O(nm)
  • 空間復(fù)雜度: O(m)

m 為最長(zhǎng)前綴長(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/longest-common-prefix/

 

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

2022-12-11 10:37:15

動(dòng)態(tài)規(guī)劃字符串超序列

2016-12-29 15:58:00

字符串子串算法

2021-12-24 09:01:05

LeetCode三數(shù)之和算法

2021-11-19 09:00:24

LeetCode字符串算法

2013-08-29 14:28:58

海量數(shù)據(jù)simhash

2012-11-16 10:15:12

算法

2013-07-03 11:31:27

2013-08-28 13:44:42

數(shù)據(jù)算法

2011-06-02 10:25:10

Web服務(wù)器部署

2011-09-23 16:22:35

2009-04-10 09:04:41

Windows 7微軟操作系統(tǒng)

2017-12-27 12:01:39

2021-04-16 16:02:13

SpringBoot分布式最大努力通知

2022-02-11 12:55:00

前綴索引索引值

2012-03-06 09:02:41

軟件開(kāi)發(fā)

2009-03-23 09:32:18

Rails 2.3.2gemruby

2020-06-24 07:43:59

公共安全IOT

2022-09-29 00:19:37

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

2011-08-16 17:14:41

Oracle數(shù)據(jù)庫(kù)OCM

2020-03-31 18:47:22

機(jī)器學(xué)習(xí)ML應(yīng)用程序
點(diǎn)贊
收藏

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