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

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

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

前言

翻轉(zhuǎn)字符串在字符串算法中算是比較常見的,而且被很多公司用作筆試題?!敝鹱址D(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í)教程之類型對(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ù),由Github的ReactiveX組織開發(fā),維護(hù)。下面這篇文章主要給大家介紹了關(guān)于RxSwift學(xué)習(xí)之基礎(chǔ)篇的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需
這篇文章主要為大家詳細(xì)介紹了Swift4.0 Array數(shù)組的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
CATransition動(dòng)畫主要在過(guò)渡時(shí)使用,比如兩個(gè)頁(yè)面層級(jí)改變的時(shí)候添加一個(gè)轉(zhuǎn)場(chǎng)效果。CATransition分為兩類,一類是公開的動(dòng)畫效果,一類是非公開的動(dòng)畫效果。這篇文章主要給大家介紹了
近日,iPhone X的發(fā)布在人們?nèi)罕娨鹆撕艽蟮霓Z動(dòng),下面這篇文章主要給大家介紹了關(guān)于利用Swift如何判斷iPhone X機(jī)型的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面
主站蜘蛛池模板: 青春草在线 | 精品国产乱码久久久久久蜜臀 | 日本a视频| 日本a级大片 | 欧美一区二区三区 | 日韩aⅴ在线观看 | 热久久999| 最新免费黄色网址 | 欧美日韩精品久久久免费观看 | 神马福利| 不卡一区二区三区四区 | 成人免费观看男女羞羞视频 | 日韩中文在线视频 | 国产一区二区在线免费 | 国产高清视频在线播放 | 日日夜夜草 | 天天操操 | 91偷拍精品一区二区三区 | 欧美一区二区三区视频在线 | 亚洲一区二区久久 | 中文在线一区 | 三级免费网 | 精品一区二区三区中文字幕 | 国产999精品久久久久久 | 久久一区二区三区电影 | 男女羞羞的网站 | 欧美高清一级片 | 国产精品成人一区二区三区夜夜夜 | 亚洲视频在线播放 | 一区二区精品电影 | 久久国产精品亚洲 | 欧美亚洲国产精品 | 久久小视频| 亚洲精品在线视频 | 日韩天堂av | 美国黄色毛片 | 免费看片在线播放 | 日日夜夜草 | 中文字幕视频网 | 亚洲精品中文字幕在线观看 | 激情欧美一区二区三区中文字幕 |