原题链接在这里: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. BZOJ1701 : [Usaco2007 Jan]Cow School牛学校

    枚举剩下的分数个数$k$,设最高的$k$个分数和的分子分母分别为$U$和$D$. 那么在选了的里面找到$A=\min(Dt[x]-Up[x])$,没选的里面找到$B=\max(Dt[x]-Up[x]) ...

  2. mysql 的 find_in_set函数使用方法

    举个例子来说: 有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1 ...

  3. storm源码之storm代码结构【译】【转】

    [原]storm源码之storm代码结构[译]  说明:本文翻译自Storm在GitHub上的官方Wiki中提供的Storm代码结构描述一节Structure of the codebase,希望对正 ...

  4. Window.location

    1.location 对象 // 假设当前url是 http://localhost/rpc/plugin.php#hash?a=aaa&b=bbb alert(window.location ...

  5. 移动端HTML5开发心得(转)

    1. iOS里fixed中有input或者textarea,用户在里面输入文字,触发键盘,fixed容器会居中显示,而不是继续悬浮       解决办法: http://dwz.cn/CrwNz 2. ...

  6. 自动备份mysql

    创建备份文件存放的目录 mkdir /usr/local/dbbak 脚本:vi /usr/local/mysqlback.sh # /bin/bash DB_NAME="dsideal_d ...

  7. 2001. Counting Sheep

      After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...

  8. HDU 4597 Play Game

    题目链接 什么都不想说,最近状态暴跌.. #include <cstdio> #include <cstring> #include <iostream> usin ...

  9. Emoji表情符号录入MySQL数据库报错

    版本一: 1,查看tomcat后台日志,核心报错信息如下:   Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\ ...

  10. Thymeleaf基本知识

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...