Reverse Words in a String 反转单词顺序
来源:互联网 发布:淘宝客qq机器人发单员 编辑:程序博客网 时间:2024/06/05 09:44
Given an input string, reverse the string word by word.
For example, Given s = "the sky is blue", return "blue is sky the".
Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space
使用API 复杂度时间 O(N) 空间 O(N)
思路将单词根据空格split开来存入一个字符串数组,然后将该数组反转即可。
注意先用trim()将前后无用的空格去掉
用正则表达式" +"来匹配一个或多个空格
代码双指针交换法 复杂度时间 O(N) 空间 O(N) 如果输入时char数组则是O(1)
思路先将字符串转换成char的数组,然后将整个数组反转。然后我们再对每一个单词单独的反转一次,方法是用两个指针记录当前单词的起始位置和终止位置,遇到空格就进入下一个单词。
代码Reverse Words in a String IIGiven 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?
时间 O(N) 空间 O(1)
思路这题就是Java版的Inplace做法了,先反转整个数组,再对每个词反转。
代码- Reverse Words in a String 反转单词顺序
- [LeetCode]Reverse Words in a String-反转单词
- [Leetcode] reverse words in a string 反转单词
- Reverse Words in a String 字符串中单词反转
- LeetCode 151. Reverse Words in a String(反转单词)
- 翻转单词顺序 Reverse Words in a String
- Reverse Words in a String 翻转单词顺序
- 151. Reverse Words in a String & 186.Reverse Words in a String II & 翻转单词顺序列
- LeetCode 186. Reverse Words in a String II(反转单词)
- LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)
- leetcode Reverse Words in a String III 反转字符串中的单词
- Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
- leetcode系列(1)Reverse Words in a String 句中单词顺序翻转
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
- Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode
- 将一个英文句子单词顺序逆置(LeetCode | Reverse Words in a String)
- LeetCode Reverse Words in a String(反转字符串)
- LeetCode Reverse Words in a String(反转字符串)
- IOS上 关于状态栏的相关设置(UIStatusBar)
- gamma 校正理解及python实现
- SVProgressHUD简单用法,非常简单(实现简单优美的 loading)
- 欢迎使用CSDN-markdown编辑器
- UE4源码分析—文件目录说明
- Reverse Words in a String 反转单词顺序
- iPhone信号强度显示----信号格与数字之间的相互切换
- Linux网络编程(五)——epoll+多线程实现简单的聊天(linux 服务器端 windows客户端)
- 深入分析:微信小程序与H5的区别
- 使用android studio添加自己写的依赖报错 安卓开发
- [博弈+线性基] BZOJ3759: Hungergame
- git版本回退
- apache和tomcat
- 基于 maven 的 ssm 项目搭建四:整合 log4j