题目:

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. hadoop下跑mapreduce程序报错

    mapreduce真的是门学问,遇到的问题逼着我把它从MRv1摸索到MRv2,从年前就牵挂在心里,连过年回家的旅途上都是心情凝重,今天终于在eclipse控制台看到了job completed suc ...

  2. 【转】给Winform的button等控件添加快捷键

    ref: http://blog.sina.com.cn/s/blog_4cb9953f0100cy4z.html 第一种:Alt + *(按钮快捷键) 在大家给button.label.menuSt ...

  3. IE6/IE7中li底部4px的Bug

    当li的子元素中有浮动(float)时,IE6/IE7中<li>元素的下面会产生4px空隙的bug. XHTML <ul class="list"> < ...

  4. 1006. Sign In and Sign Out

    #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h& ...

  5. 十一、mysql输入安全

    .尽量使用“绑定参数”功能,php中可用pdo进行一系列操作 .php可使用mysql_real_escape_string()函数进行输入过滤:

  6. P3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二

    这题..思维上远没有上一题复杂,是一个裸的RMQ..利用倍增就可以解决了. var n,q,i,j,f,t,c:longint; a:array[..,..] of longint; function ...

  7. Qt单元测试

    单元测试之作用要完成测试用例,保证设计上的耦合依赖通过测试用例,保证覆盖率,提高程序质量 QTest一些有用的静态函数QTest::qExecQTest::qSleepQTest::qWait   例 ...

  8. PD code与name联动(取消)设置

    在powerdesign中,code与name老是联动,修改了name中的数据,code随之修改,影响效率,设置Tools-General Options-Dialog  中的Name to Code ...

  9. yum安装gcc

    如果服务器是自己的,并且机器就在身边,那什么都不用说了,缺少gcc顶多就是重新放入安装盘,把开发工具包安装上.但是如果是租的服务器,托管服务 方那帮人又搞不懂你说的啥子gcc,要安装gcc实在是太麻烦 ...

  10. 你不需要jQuery(二)

    完全没有否定jQuery的意思,jQuery是一个神奇的.非常有用的工具,可以节省我们大量的时间. 但是,有些时候,我们只需要jQuery的一个小功能,来完成一个小任务,完全没有必要加载整个jQuer ...