Swift算法实现逐字翻转字符串的方法示例
2017-03-21 11:11
726 查看
翻转字符串在字符串算法中算是比较常见的,而且被很多公司用作笔试题。”逐字翻转字符串”是翻转字符串的翻版,也是之前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算法实现逐字翻转字符串的方法,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
您可能感兴趣的文章:
相关文章推荐
- Swift算法实现字符串转数字的方法示例
- Swift算法之二叉树实现的方法示例
- swift 3.0中实现字符串截取、比较的方法示例
- PHP实现字符串翻转功能的方法【递归与循环算法】
- PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
- Python对字符串实现去重操作的方法示例
- php中实现字符串翻转的方法
- Java实现字符串解析为日期时间的方法示例
- Objective-C与Swift中通过字符串获取类的方法对比与实现
- php简单实现多字节字符串翻转的方法
- 自己写方法,实现字符串翻转
- java实现字符串转String数组的方法示例
- C#实现翻转字符串的方法
- Python实现的破解字符串找茬游戏算法示例
- 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)
- Java基于余弦方法实现的计算相似度算法示例
- javascript实现根据函数名称字符串动态执行函数的方法示例
- Java 其中翻转字符串的实现方法
- 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。 给定一个string iniString,请返回一个string,为翻转后的字符串。保证字符串的
- php实现字符串翻转的方法