题目:

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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 186. Reverse Words in a String II 翻转有空格的单词串 里面不变

    [抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...

  5. 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...

  6. [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 ...

  7. 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 ...

  8. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  9. [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 ...

随机推荐

  1. 问题解决:两台虚拟机不能互相ping通的原因

    要是两台虚拟机能够PING通下列要求缺一不可:1.你所设置的虚拟网络的网络号不能跟外面你正在使用的真实的网络号一样2.防火墙必须关闭3.你设置的那俩台虚拟机必须在同一网段内4.两台虚拟机的主机名不能相 ...

  2. linux gcc 和 g++ 编译

    gcc编译 gcc -o test.out test.c g++ 编译 g++ -o test.out test.cpp

  3. 在MAC OS X下安装usb转串口驱动(PL2303主控芯片)

    本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重!       因为最近手里有一块STM32Discovery开发板,所以想搞一下STM32的开发,我前面的 ...

  4. 英文版firefox显示中文字体丑的问题

    在Options里面选择Content,在Fonts&Colors区域的Default font中,选择Times New Roman 如下图1: 在旁边的Advanced中选择,Fonts ...

  5. StreamReader和StreamWrite与FileStream区别

    具体用法不在赘述. 记录一下用法的区别 StreamReader: FileStream fs = new FileStream(@"D:\Readme.txt",FileMode ...

  6. Qt模拟C#的File类对文件进行操作

    其实只是QT菜鸟为了练习而搞出来的 文件头: #include <QFile> #include <QString> #include <iostream> #in ...

  7. 用phpmailer发邮件 中文乱码问题解决

    加入如下代码解决. $mail->CharSet = "GB2312"; //utf-8; $mail->Encoding = "base64"; ...

  8. Android笔记——Bitmap自动取色(纯搬运)

    2015/6/12更新:发现一个更好的,带demo https://github.com/MichaelEvans/ColorArt 说明: 这个是一个老外写的自动自动从bitmap中取主色与第二主色 ...

  9. 十、mysql事务的简介

    1. myisam跟memory支持表级别锁定 BDB 页级锁定 Innodb 行级锁定 2.表锁(不是表嫂哈) lock table read //只读表锁,也就是说执行了这个锁后,锁内的操作只能为 ...

  10. linux命令后面常见的>/dev/null 和 2>&1 的含义

    >/dev/null 输出到空设备,表示丢掉输出信息. 2 > &1 将输出到标准错误的信息输出到标准输出设备(通常是屏幕) 有3个默认的i/o, 0 是标准输入,一般是键盘 1 ...