国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

Swift算法實現逐字翻轉字符串的方法示例

2020-03-09 17:45:51
字體:
來源:轉載
供稿:網友

前言

翻轉字符串在字符串算法中算是比較常見的,而且被很多公司用作筆試題。”逐字翻轉字符串”是翻轉字符串的翻版,也是之前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"

接下來看下實現思路和代碼。

實現思路及代碼

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

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

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

所以,首先可以實現一個可以翻轉局部和全部字符串的算法,傳入字符數組、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算法實現逐字翻轉字符串的方法,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石首市| 徐闻县| 兴义市| 正镶白旗| 新河县| 壤塘县| 韶山市| 洮南市| 南皮县| 潞西市| 连城县| 永胜县| 新巴尔虎左旗| 余姚市| 菏泽市| 康马县| 广饶县| 济南市| 卢氏县| 民勤县| 开封县| 讷河市| 仁寿县| 昌黎县| 绥宁县| 大化| 钦州市| 南木林县| 墨竹工卡县| 疏附县| 武川县| 县级市| 阿拉善盟| 翼城县| 北川| 黄冈市| 镇巴县| 乡宁县| 鲜城| 定远县| 神农架林区|