欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年03月21日 11:11:36   作者:李峰峰博客  
大家都知道翻轉字符串在字符串算法中算是比較常見的,下面這篇文章主要介紹了Swift算法實現(xià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?

簡而言之就是:”the sky is blue”—>”blue is sky the”

所以,對于本文,要解決的算法是:

逐字翻轉字符串,例如:"the sky is blue"—>"blue is sky the"

接下來看下實現(xiàn)思路和代碼。

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

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

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

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

所以,首先可以實現(xiàn)一個可以翻轉局部和全部字符串的算法,傳入字符數(shù)組、startIndex 和 endIndex ,其中 startIndex 和 endIndex 分別為要翻轉的字符串的起始下標和結束下標,也就是要翻轉 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)
  
 }
 
}

之后就可以利用上面的算法去完成前面說的兩次翻轉:

func reverseWords(_ str:String) -> String{
 
 var chars = [Character](str.characters)
 
 //首先翻轉整個字符串所有字符,"the sky is blue" -> "eulb si yks eht"
 _reverseStr(&chars,0,chars.count-1)
 
 //然后翻轉每個單詞中的字符,"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)
}

完整算法代碼:

//翻轉指定范圍的字符
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)
  
 }
 
}
 
//逐字翻轉字符串
func reverseWords(_ str:String) -> String{
 
 var chars = [Character](str.characters)
 
 //首先翻轉整個字符串所有字符,"the sky is blue" -> "eulb si yks eht"
 _reverseStr(&chars,0,chars.count-1)
 
 //然后翻轉每個單詞中的字符,"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"

總結

以上就是關于Swift算法實現(xiàn)逐字翻轉字符串的方法,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • Swift map和filter函數(shù)原型基礎示例

    Swift map和filter函數(shù)原型基礎示例

    這篇文章主要為大家介紹了Swift map和filter函數(shù)原型基礎示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Swift 常量與變量實例詳解

    Swift 常量與變量實例詳解

    這篇文章主要介紹了Swift 常量與變量實例詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • SwiftUI 登錄界面布局實現(xiàn)示例詳解

    SwiftUI 登錄界面布局實現(xiàn)示例詳解

    這篇文章主要為大家介紹了SwiftUI 登錄界面布局實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Swift Extension擴展得使用詳細介紹

    Swift Extension擴展得使用詳細介紹

    在swift中,extension與Objective-C的category有點類似,但是extension比起category來說更加強大和靈活,它不僅可以擴展某種類型或結構體的方法,同時它還可以與protocol等結合使用,編寫出更加靈活和強大的代碼
    2022-09-09
  • 解決 Xcode 6-Beta2 智能提示bug

    解決 Xcode 6-Beta2 智能提示bug

    最近開始學習 Swift ,因為感覺這個真是個不錯的東西,有很多新的特性,雖然 Titanium 之后也必定會支持,但總有不少東西要使用原生開發(fā)才可以實現(xiàn),所以就乘這個新語言出來之際開始學習啦!
    2014-07-07
  • Swift利用CoreData如何存儲多種數(shù)據(jù)類的通訊錄

    Swift利用CoreData如何存儲多種數(shù)據(jù)類的通訊錄

    這篇文章主要給大家介紹了關于Swift利用CoreData如何存儲多種數(shù)據(jù)類的通訊錄的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。
    2017-12-12
  • SwiftUI智能家居開關燈頁面搭建示例

    SwiftUI智能家居開關燈頁面搭建示例

    這篇文章主要為大家介紹了SwiftUI智能家居開關燈頁面搭建示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 窺探Swift編程中的錯誤處理與異常拋出

    窺探Swift編程中的錯誤處理與異常拋出

    本文給大家整理些關于Swift編程中的錯誤處理與異常拋出,本文介紹的非常詳細,基于參考價值,特此分享腳本之家平臺供大家學習
    2016-02-02
  • Swift利用CoreData實現(xiàn)一個上班簽到的小工具

    Swift利用CoreData實現(xiàn)一個上班簽到的小工具

    這篇文章主要給大家介紹了關于Swift利用CoreData實現(xiàn)一個上班簽到小工具的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • Swift?中的?JSON?反序列化示例詳解

    Swift?中的?JSON?反序列化示例詳解

    這篇文章主要為大家介紹了Swift中的JSON?反序列化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07

最新評論