久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Swift算法實(shí)現(xiàn)逐字翻轉(zhuǎn)字符串的方法示例

大家都知道翻轉(zhuǎn)字符串在字符串算法中算是比較常見(jiàn)的,下面這篇文章主要介紹了Swift算法實(shí)現(xiàn)逐字翻轉(zhuǎn)字符串的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來(lái)一

前言

翻轉(zhuǎn)字符串在字符串算法中算是比較常見(jiàn)的,而且被很多公司用作筆試題。”逐字翻轉(zhuǎn)字符串”是翻轉(zhuǎn)字符串的翻版,也是之前Google的面試題,原題是這樣的:


Given an input string, reverse the string word by word.
A word is defined as a sequence of non-space characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?

簡(jiǎn)而言之就是:”the sky is blue”—>”blue is sky the”

所以,對(duì)于本文,要解決的算法是:

逐字翻轉(zhuǎn)字符串,例如:"the sky is blue"—>"blue is sky the"

接下來(lái)看下實(shí)現(xiàn)思路和代碼。

實(shí)現(xiàn)思路及代碼

既然是字符串翻轉(zhuǎn)的翻版,我們就可以利用之前翻版字符串的思路去解決就可以了,不過(guò)這道題要有兩次翻轉(zhuǎn):

第一次翻轉(zhuǎn),整體翻轉(zhuǎn):”the sky is blue” -> “eulb si yks eht”

第二次翻轉(zhuǎn),單詞翻轉(zhuǎn):”eulb si yks eht” -> “blue is sky the”

所以,首先可以實(shí)現(xiàn)一個(gè)可以翻轉(zhuǎn)局部和全部字符串的算法,傳入字符數(shù)組、startIndex 和 endIndex ,其中 startIndex 和 endIndex 分別為要翻轉(zhuǎn)的字符串的起始下標(biāo)和結(jié)束下標(biāo),也就是要翻轉(zhuǎn) startIndex 和 endIndex 之間(包含)的字符,代碼如下:


func _reverseStr( _ chars:inout [Character], _ startIndex:Int, _ endIndex:Int){
 
 var startIndex = startIndex
 var endIndex = endIndex
 
 if startIndex <= endIndex {
  
  let tempChar = chars[endIndex]
  chars[endIndex] = chars[startIndex]
  chars[startIndex] = tempChar
  
  startIndex += 1
  endIndex -= 1
  
  _reverseStr(&chars,startIndex,endIndex)
  
 }
 
}

之后就可以利用上面的算法去完成前面說(shuō)的兩次翻轉(zhuǎn):


func reverseWords(_ str:String) -> String{
 
 var chars = [Character](str.characters)
 
 //首先翻轉(zhuǎn)整個(gè)字符串所有字符,"the sky is blue" -> "eulb si yks eht"
 _reverseStr(&chars,0,chars.count-1)
 
 //然后翻轉(zhuǎn)每個(gè)單詞中的字符,"eulb si yks eht" -> "blue is sky the"
 var startIndex = 0
 for endIndex in 0 ..< chars.count {
  if endIndex == chars.count - 1 || chars[endIndex + 1] == " " {
   _reverseStr(&chars, startIndex, endIndex)
   startIndex = endIndex + 2
  }
 }
 
 return String(chars)
}

完整算法代碼:


//翻轉(zhuǎn)指定范圍的字符
func _reverseStr( _ chars:inout [Character], _ startIndex:Int, _ endIndex:Int){
 
 var startIndex = startIndex
 var endIndex = endIndex
 
 if startIndex <= endIndex {
  
  let tempChar = chars[endIndex]
  chars[endIndex] = chars[startIndex]
  chars[startIndex] = tempChar
  
  startIndex += 1
  endIndex -= 1
  
  _reverseStr(&chars,startIndex,endIndex)
  
 }
 
}
 
//逐字翻轉(zhuǎn)字符串
func reverseWords(_ str:String) -> String{
 
 var chars = [Character](str.characters)
 
 //首先翻轉(zhuǎn)整個(gè)字符串所有字符,"the sky is blue" -> "eulb si yks eht"
 _reverseStr(&chars,0,chars.count-1)
 
 //然后翻轉(zhuǎn)每個(gè)單詞中的字符,"eulb si yks eht" -> "blue is sky the"
 var startIndex = 0
 for endIndex in 0 ..< chars.count {
  if endIndex == chars.count - 1 || chars[endIndex + 1] == " " {
   _reverseStr(&chars, startIndex, endIndex)
   startIndex = endIndex + 2
  }
 }
 
 return String(chars)
}
 
reverseWords("the sky is blue") //return "blue is sky the"

總結(jié)

以上就是關(guān)于Swift算法實(shí)現(xiàn)逐字翻轉(zhuǎn)字符串的方法,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)html5模板網(wǎng)的支持。

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

這篇文章主要給大家介紹了關(guān)于RxSwift學(xué)習(xí)教程之類(lèi)型對(duì)象Subject的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著
這篇文章主要給大家介紹了關(guān)于RxSwift學(xué)習(xí)教程之Observable的相關(guān)資料,文中詳細(xì)的給大家介紹了關(guān)于新建Observable、訂閱Observable和取消訂閱并消除內(nèi)存泄漏等相關(guān)的內(nèi)容,需要的朋友可以
RxSwift是Swift函數(shù)響應(yīng)式編程的一個(gè)開(kāi)源庫(kù),由Github的ReactiveX組織開(kāi)發(fā),維護(hù)。下面這篇文章主要給大家介紹了關(guān)于RxSwift學(xué)習(xí)之基礎(chǔ)篇的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需
這篇文章主要為大家詳細(xì)介紹了Swift4.0 Array數(shù)組的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
CATransition動(dòng)畫(huà)主要在過(guò)渡時(shí)使用,比如兩個(gè)頁(yè)面層級(jí)改變的時(shí)候添加一個(gè)轉(zhuǎn)場(chǎng)效果。CATransition分為兩類(lèi),一類(lèi)是公開(kāi)的動(dòng)畫(huà)效果,一類(lèi)是非公開(kāi)的動(dòng)畫(huà)效果。這篇文章主要給大家介紹了
近日,iPhone X的發(fā)布在人們?nèi)罕娨鹆撕艽蟮霓Z動(dòng),下面這篇文章主要給大家介紹了關(guān)于利用Swift如何判斷iPhone X機(jī)型的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面
主站蜘蛛池模板: av在线视 | av免费网站在线观看 | 国产成人一区二区三区久久久 | 美女福利视频网站 | 操人网站| 欧美日韩精品一区二区天天拍 | 91免费在线| 欧美午夜精品久久久久免费视 | 亚洲视频一 | 最近中文字幕在线视频1 | 在线观看日韩精品视频 | 欧美日韩在线成人 | 欧美午夜精品 | 亚洲国产一区二区三区 | 在线日韩视频 | 国产成人精品一区二区三区在线 | av影音资源| 国产精品99久久久久久久久 | 毛片a级| www.久久国产精品 | 丝袜美腿一区二区三区动态图 | 成人精品免费视频 | 日韩在线免费视频 | 涩涩视频网站在线观看 | 国产伦精品一区二区三区照片91 | 日韩久久精品视频 | 欧美精品一区二区在线观看 | 中文字幕视频在线观看 | 男女羞羞视频在线免费观看 | 成人在线免费视频 | 97人人干 | 在线欧美小视频 | 国产成人一区二区三区久久久 | 97久久久| 日韩精品亚洲专区在线观看 | 国产精品久久久久久吹潮 | 都市激情亚洲 | 中文字幕日韩一区 | 日本成人福利 | 日韩精品一二三区 | 国产精品爱久久久久久久 |