Difficulty: Medium

 More:【目录】LeetCode Java实现

Description

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

Example:

Input: "the sky is blue",
Output: "blue is sky the".

Note:

  • A word is defined as a sequence of non-space characters.
  • Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
  • You need to reduce multiple spaces between two words to a single space in the reversed string.

Follow up: For C programmers, try to solve it in-place in O(1) space.

Intuition

Use two pointers, startint from the end to the begining of the string to get each word.

Make use of StringBuilder to append each word.

Solution

    public String reverseWords(String s) {
if(s==null || s.length()==0)
return s;
StringBuilder sb= new StringBuilder();
int j=s.length();
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)==' ')
j=i;
else if(i==0 || s.charAt(i-1)==' '){
if(sb.length()!=0)
sb.append(" ");
sb.append(s.substring(i,j));
}
}
return sb.toString();
}

Complexity

Time complexity : O(n).

Space complexity :  O(n)

What I've learned

1. There is only one space between two words in the reversed string.

2. Learn how to use two pointers to get each word.

 More:【目录】LeetCode Java实现

【LeetCode】151. Reverse Words in a String的更多相关文章

  1. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  2. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  3. 【刷题-LeetCode】151 Reverse Words in a String

    Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...

  4. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  5. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  6. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

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

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

  8. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  9. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

随机推荐

  1. isspace 对含有中文 的字符串进行检查的时候表现不正常!?

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...

  2. 2:jquery.cookie用法详细解析

    一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. ...

  3. Qt error ------ 'XXX' has not been declared

    1.头文件没加 2.调用函数者的头文件在XXX头文件的下方

  4. 构造方法和一般方法的区别(面试)-----java基础知识总结

    构造方法:对象创建时,就会调用与之对应的构造方法,对对象进行初始化: 一般方法:对象创建后,需要方法功能时才会调用. 构造方法,在对象创建时,会调用且只调用一次. 一般方法,对象创建后,可以被调用多次 ...

  5. 在线Python学习网站

    目前我们使用的Python集成环境是Anaconda3,然后使用Jupyter Notebook和Spyder两个开发环境 Goole推出了在线的开发环境,在线网站: https://colab.re ...

  6. vue之props父子组件之间的谈话

    眨眼就来杭州两年了,时间真快. 我们今天来说说vue的一个api---->props 首先我们先看看一个例子,是我一个项目中写的. 看到这个:有木有一点懂了.要是没懂,继续往下看 这里我们用到了 ...

  7. Spark记录-Scala函数与闭包

    函数声明 Scala函数声明具有以下形式 - def functionName ([list of parameters]) : [return type] Scala 如果不使用等号和方法体,则隐式 ...

  8. 【DS】排序算法之快速排序(Quick Sort)

    一.算法思想 快速排序,顾名思义,效率比较于其他算法,效率比较高.<算法导论>也专门对其进行讲解.其算法设计使用分治思想,如下: 1)从数组A[p...r]中选择一个元素,将数组划分成两个 ...

  9. bzoj千题计划287:bzoj1228: [SDOI2009]E&D

    http://www.lydsy.com/JudgeOnline/problem.php?id=1228 打SG函数表,找规律: 若n是奇数m是奇数,则SG(n,m)=0 若n是偶数m是偶数,则SG( ...

  10. 浏览器存储:cookie

    Cookie是什么:cookie是指存储在用户本地终端上的数据,同时它是与具体的web页面或者站点相关的.Cookie数据会自动在web浏览器和web服务器之间传输,也就是说HTTP请求发送时,会把保 ...