原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/

题目:

Given an input string , reverse the string word by word.

Example:

Input:  ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]

Note:

  • A word is defined as a sequence of non-space characters.
  • The input string does not contain leading or trailing spaces.
  • The words are always separated by a single space.

Follow up: Could you do it in-place without allocating extra space?

题解:

input变成 char array.

先reverse 全部 char array. 从头往后走,遇到空格或者array尾部,就reverse中间的单词.

Note: Do NOT forget i == n, reverse.

Time Compelxity: O(s.length()). Space: O(1).

AC Java:

 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 = 1; 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 temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}

类似Reverse Words in a String.

跟上Reverse Words in a String IIIRotate Array.

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

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

  2. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  5. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

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

  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 III

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...

  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. 使用CSS3的appearance属性改变元素的外观

    昨天在和同事一起完成项目的时候,我使用了appearance来渲染select,但是在firefox下出现问题,不完美,最后去除了.但还是要学习下这个属性.大家都知道每个浏览器对HTML元素渲染都不一 ...

  2. hdu 1312 Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  3. A Simple C++ Template Class that Matches a String to a Wildcard Pattern

    A recently implemented enhanced wildcard string matcher, features of which including, Supporting wil ...

  4. js两个小技巧【看到了就记录一下】

    1.不声明第三个变量实现交换 ,b=; a=[b,b=a][];//执行完这句代码之后 a的值为2 b的值为1了 2.&&和||的用法 (学会了立马感觉高大尚了吧) ; //传统if语 ...

  5. Android -- EditText方法

    1. 选择需要的内容 final EditText myEditText=(EditText)findViewById(R.id.editText1); //编辑框对象 myEditText.setT ...

  6. SQLServer大数据优化方法若干

    1.使用ndf文件. 自从sqlserver2005后,默认不增生成ndf文件. mdf:priMary Data file ldf:Log Data File ndf:secoNdary data ...

  7. 省略文字的css

    在显示一行文字时,如果容器太小,为了显示出省略字符,可以使用 ellipsis { white-space: nowrap; overflow: hidden; text-overflow: elli ...

  8. Crystal Reports 2008(水晶报表) 安装

    这篇blog主要是介绍Crystal Reports2008(水晶报表)的安装. 首先我们应该知道Crystal Reports 有什么作用? 从这里Crystal Reports 你可以了解到它的一 ...

  9. Linux下rz命令使用的实例详解

    Linux中rz命令和sz命令都可用于文件传输,而rz命令主要用于文件的上传,下面将通过几个实例来给大家详细介绍下Linux下rz命令的用法,一起来学习下吧. rz命令可以批量上传文件,当然也可上传单 ...

  10. Hadoop.2.x_源码编译

    一.基本环境搭建 1. 准备 hadoop-2.5.0-src.tar.gz apache-maven-3.0.5-bin.tar.gz jdk-7u67-linux-x64.tar.gz proto ...