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

For example,

Given s = "the sky is blue",

return "blue is sky the".

Clarification:

What constitutes a word?

A sequence of non-space characters constitutes a word.

Could the input string contain leading or trailing spaces?

Yes. However, your reversed string should not contain leading or trailing spaces.

How about multiple spaces between two words?

Reduce them to a single space in the reversed string.

中文:给定一个输入字符串,依照单词逆转之。

比如:

给定 s="the sky is blue",

返回 "blue is sky the"

说明:什么组成一个单词?

一个非空的字符序列组成一个单词。

输入的字符串能够包括头或尾的空格吗?

能够。可是你的倒转的字符串不应该包括头尾空格。

两个单词间有多个空格又如何呢?

在倒转的字符串中把它们降低为一个空格。

此题比較简单,主要考虑使用空格进行切割和空格的去除。

Java:

	  public String reverseWords(String s) {
if(s.trim() == "")
return null;
String str[] = s.trim().split("\\s+");
StringBuilder sb = new StringBuilder();
int len = str.length;
for(int i=len-1;i>=0;i--){
if(str[i] == " ")
continue;
sb.append(str[i]+" ");
}
return sb.toString().trim();
}

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

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

  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 II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

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

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

  5. LeetCode Reverse Words in a String II

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

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

  7. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  8. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

  9. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  10. [leetcode]Reverse Words in a String @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...

随机推荐

  1. JConsole 连接配置

    远程监控配置 JDK配置 在%JAVA_HOME%/jre/lib/management目录下,jmxremote.password.template.jmxremote.password需要修改配置 ...

  2. Guava源码分析——ServiceManager

    ServiceManager类:      用于监控服务集的管理器,该类提供了诸如startAsync.stopAsync.servicesByState方法来运行.结束和检查服务集,而且,通过监听器 ...

  3. Servlet实现Session

    (1)首先看一下项目的结构 是在tomcat--webaps下的myWebSites项目 在myWebSites下有仅仅有WEB-INF目录 在WEB-INF目录中有  一下目录(在classes目录 ...

  4. 数据库的四种语言(DDL、DML、DCL、TCL)

    1.DDL (Data Definition Language )数据库定义语言 statements are used to define the database structure or sch ...

  5. HTML基础知识笔记(一)

    HTML定义 HTML指的是超文本标记语言 HTML不是编程语言,而是标记语言 标记语言是一套标记标签 HTML是用标记标签来描述网页   HTML标签1 <html></html& ...

  6. jquery插件datepicker

    jQuery UI很强大,其中的日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式.语言.限制选择日期范围.添加相关按钮以及其它导航等. <script ...

  7. OD调试2---TraceMe

    OD调试2---TraceMe 拆解一个Windows程序要比拆解一个DOS程序容易得多,因为在Windows中,只要API函数被使用,想对寻找蛛丝马迹的人隐藏一些东西是比较困难的.因此分析一个程序, ...

  8. javaScript事件机制兼容【整理】

    [添加事件机制]  addEventListener  和  attachEvent [W3C] addEventListener('click' , function(){alert('Hello ...

  9. [转载]启用 VIM 中的 Python 自动补全及提示功能

    转载: http://zhongwei-leg.iteye.com/blog/941474 周围的同事不喜欢使用 VIM 写 Python 代码的原因之一就是,VIM 不能像 Visual Studi ...

  10. js跨浏览器事件处理

    var EventUtil = { addHandler: function(element,type,handler){ if(element.addEventListener){ element. ...