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?

先把每个词反转一遍, 再把整个string 反转一遍。

 public class Solution {
public void reverseWords(char[] s) {
for(int i = 0, j = 0; j <= s.length && i < s.length; j ++){
if(j == s.length || s[j] == ' '){
reverse(s, i, j - 1);
i = j + 1;
}
}
reverse(s, 0, s.length - 1);
} private void reverse(char[] c, int s, int e){
while(s < e){
char tmp = c[s];
c[s] = c[e];
c[e] = tmp;
s ++; e --;
}
}
}

Reverse Words in a String II的更多相关文章

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

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

  3. LeetCode Reverse Words in a String II

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

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

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

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

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

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

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

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

随机推荐

  1. [SCOI2010]传送带 三分法

    [SCOI2010]传送带 LG传送门 三分法模板. 关于为什么可以三分,我选择感性理解,有人证明了,总之我是懒得证了. 假设路径是\(A \to E \to F \to D\),\(E\)和\(F\ ...

  2. 奇技淫巧之 unordered_map

    哇,虽然我不知道到为什么这个东西比map快(快了一倍),注意unordered_map要在c++11以上的编译环境下才能用,如c++11,GNU G++11 5.1.0,GNU G++14 .

  3. 关于自动化测试框架,所需代码技能,Java篇——参数配置与读取.

    前言: 说在前边.像我这种假期不出去浪,在这里乖乖写文章研究代码的人,绝壁不是因为爱学习,而是自己不知道去哪玩好,而且也不想玩游戏,看电视剧什么的,结果就无聊到看代码了…… 至于如何解读代码,请把它当 ...

  4. Jupyter安装及运行

    一.安装(来自http://jupyter.org/install) Ctrl+Alt+T(Manjaro系统),打开控制台,su进入root权限 输入如下命令: python3 -m pip ins ...

  5. 如何配置pycaffe

    首先,使用cmake配置.生成caffe的vs2015工程时,设定生成python接口,即BUILD项->BUILD_python.BUILD_python_layer,注意使用CMake生成V ...

  6. To Do List | 事实上是咕咕咕计划

    1.写一两篇关于数学的博文 类似于这种反演啥的或者说是FFT一些更本质的东西趴...反正是我根本不会的东西 再写一点自己会的东西趴...(好像也只有什么课本上的东西讲讲了,不过应该会写一些自己曾经发现 ...

  7. ubuntu下import matplotlib错误解决办法

    环境:ubuntu16.04,python2.7,tensorflow1.4.0 问题: ImportError: No moudule named _tkinter, please install ...

  8. c++ getline()和get()的区别

    1.方法get(char &)和get(void)提供不跳过空白的单字符输入功能:2.函数get(char * , int , char)和getline(char * , int , cha ...

  9. Python列表解析

    列表解析 根据已有列表,高效创建新列表的方式. 列表解析是Python迭代机制的一种应用,它常用于实现创建新的列表,因此用在[]中. 语法: [expression for iter_val in i ...

  10. MyForm_参考django的Form组建

    fork wupeiqi:https://github.com/fat39/Tyrion 组件说明:https://www.cnblogs.com/wupeiqi/p/5938916.html