186. Reverse Words in a String II
题目:
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?
链接: http://leetcode.com/problems/reverse-words-in-a-string-ii/
题解:
翻转单词II。这次给定了char array,我们就可以使用三步翻转法了。因为题目的条件很优惠,前后没有空格,单词间又只有一个空格,那我们可以省略很多边界条件的判定,先翻转整个数组,然后碰到单词就正序回来。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public void reverseWords(char[] s) {
if(s == null || s.length == 0)
return;
reverse(s, 0, s.length - 1);
int lo = 0;
for(int i = 0; i <= s.length; i++) {
if(i == s.length || s[i] == ' ') {
reverse(s, lo, i - 1);
lo = i + 1;
}
}
}
private void reverse(char[] s, int i, int j) {
while(i < j)
swap(s, i++, j--);
}
private void swap(char[] s, int i, int j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
题外话:
同事要去南极玩,先飞到阿根廷,再到乌斯怀亚,然后坐科考船过去。科考船一套大概10500刀,机票另算,好爽啊。以后我也要好好去旅游。
二刷:
跟一刷基本一样
Java:
public class Solution {
public void reverseWords(char[] s) {
if (s == null || s.length < 2) return;
int len = s.length;
reverse(s, 0, len - 1);
int lo = 0;
for (int i = 0; i < len; i++) {
if (s[i] == ' ') {
reverse(s, lo, i - 1);
lo = i + 1;
}
}
reverse(s, lo, len - 1);
}
private void reverse(char[] s, int lo, int hi) {
while (lo < hi) {
char tmp = s[lo];
s[lo] = s[hi];
s[hi] = tmp;
lo++;
hi--;
}
}
}
Reference:
http://www.zhihu.com/question/19857821
186. Reverse Words in a String II的更多相关文章
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- Leetcode - 186 Reverse Words in a String II
题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...
- 186. Reverse Words in a String II 翻转有空格的单词串 里面不变
[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- Reverse Words in a String I & Reverse Words in a String II
Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [Swift]LeetCode186. 翻转字符串中的单词 II $ Reverse Words in a String II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
随机推荐
- root-systerm-bin是什么program
root-systerm-bin是什么program http://packages.ubuntu.com/lucid/root-system-bin
- lex&yacc5--YYSTYPE
yacc里的YYSTYPE默认是int型的,当然也可以勇%union来定义联合但是由于程序需要,我要将YYSTYPE定义为我自己定义的一个struct的指针然后作为一个全局变量,让lex在扫描的时候, ...
- 指针与strncpy---内存
指针的形式的赋值和strncpy的赋值 e.SetAttr("Amt", ToString(dAmt) ); e.SetAttr("Amt", sAm ...
- Adobe Illustrator CS6 绿色简体中文版下载地址
一.Adobe Illustrator CS6 简体中文精简绿色优化版:1.由官方简体中文正式版制作而成,只需要执行一次快速安装即可使用.已经注册,非tryout版,支持x64位系统.2.精简了Ext ...
- Hash function
Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers fr ...
- 导出excel表格
一. 1.获取数据源2.DataTable dt = st.Tables[0]; HttpResponse resp; // HTTP响应信息 resp = Page.Response; resp.C ...
- 【Android】Android SDK在线更新镜像服务器
Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/IPV6: http://mi ...
- MFC的规则DLL与扩展DLL
一.MFC规则DLL MFC规则DLL可以在该dll内部使用MFC,但是与应用程序的接口不能是MFC的.能够被所有支持dll的编程语言所写的应用程序使用,当然也包括使用MFC创建的应用程序.在 ...
- Popup window
function createLoadingDialog() { $("#loadingDialog").dialog({ autoOpen: false, closeOnEsca ...
- asp.net webform download excel
private void exportBinaryToExcel(byte[] bytes, string filename) { Response.AddHeader("Content-D ...