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.

思路

1.  split by a single or multiple spaces, making sure remove leading or trailing spaces

2. to reverse,  traverse the input from right to left,  append each item into StringBuilder

代码

 public class Solution {
public String reverseWords(String s) {
// corner case
if(s == null || s.length() == 0) return "";
// s.trim() 去除leading or trailing spaces
// s.split("\\s+")根据a single or multiple spaces 来split字符串
String[] words = s.trim().split("\\s+");
// use StringBuilder to save result
StringBuilder sb = new StringBuilder();
// to reverse, from right to left
for(int i = words.length-1; i >= 0; i--){
sb.append(words[i]+" ");
}
// 确保结果中去除了leading or trailing spaces
return sb.toString().trim();
}
}

[leetcode]151. Reverse Words in a String翻转给定字符串中的单词的更多相关文章

  1. LeetCode 557. Reverse Words in a String III (反转字符串中的单词 III)

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

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

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

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

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

  4. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  5. LeetCode 151 reverse word in a string

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

  6. Java for LeetCode 151 Reverse Words in a String

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

  7. leetcode 151. Reverse Words in a String --------- java

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

  8. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

  9. Leetcode#151 Reverse Words in a String

    原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...

随机推荐

  1. Django下的templates 和 static静态文件

    如果Django顶层目录中没有templates的话,就自己新建一个Directory ,这个文件是存放html文件的 1)如果在views里面用render(request,"" ...

  2. css:margin和padding的百分之使用

    #app { position: fixed; width: 94%; height: 100%; background: pink; padding: 0px 3% 0px 3%;} 如上代码,最终 ...

  3. Java读写hdfs上的avro文件

    1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...

  4. Java重写equals方法(重点讲解)

    为什么equals()方法要重写? 判断两个对象在逻辑上是否相等,如根据类的成员变量来判断两个类的实例是否相等,而继承Object中的equals方法只能判断两个引用变量是否是同一个对象.这样我们往往 ...

  5. webpack+avalon+mmState打包方案

    终于到讲授如何整合avalon社区这个最强大的组件,基于状态机的路由系统了! 基于状态机的路由系统,据我所知,目前世界上只有三款,angular社区的ui-router, 网易出品的stateman, ...

  6. thread == 票池

    public class ThreadDemo2 { public static void main(String[] args){ TicketPool tp = new TicketPool(); ...

  7. web前端基础知识!

    [HTML文档的基本结构和语法][基本结构]: <HTML> HTML 文件开始 <HEAD> HTML 文件的头部开始 <title> 网页的标题</tit ...

  8. [转载]FMS Dev Guide学习笔记(验证客户端二)

    一.开发交互式的媒体应用程序 1.使用unique key a. 在客户端ActionScript中创建一个unique key,如下代码所示,unique key的组成为本地电脑时间和一个随机数连接 ...

  9. linux 内核根文件系统

    参考: http://blog.csdn.net/guopeixin/article/details/5962482 http://www.yunweipai.com/archives/1184.ht ...

  10. jquery 中attr()的一个用法

    html 如下: <ul><li><img src="./img/addface_icon.png" alt="">< ...