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. Java动态 遍历List 时删除List特征元素 异常问题 及解决方案总结

    首先.这是一个极其简单的问题,大牛可忽略.新手可能会遇到,Java中遍历某个List 时删除该List元素 会抛出异常. 这一个简单的问题再高手严重不值一提,但新手可能会比較困惑,用哪种方式能够安全有 ...

  2. 在Unity3d编辑器中加入菜单以及菜单项

    在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! using ...

  3. mbed OS - ARM关于物联网(IoT)的战略布局

    关于IoT 在刚刚过去的ARMTECHCON2014(Santa Clara Convention Center)第1天会议,首要的keynote就是ARM针对建立物联网(InternetOf Thi ...

  4. Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表)

    Sql Server 函数的操作实例!(执行多条语句,返回Select查询后的临时表) SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ==== ...

  5. 在OC和Swift中使用IBDesignable/IBInspectable

    iOS8新特性IBDesignable/IBInspectable,可以直接在XIB或者Storyboard中直接,设置UI类的属性.例 如:UIView.layer.borderWidth.bord ...

  6. Java中出现“错误: 编码GBK的不可映射字符”的解决方法

    我的java文件里出现中文,是这样一个文件: import java.io.*; public class Test { public static void main(String[] args) ...

  7. [HeadFirst-HTMLCSS学习笔记][第六章严格的HTML]

    远古 古老的html 4.01和XHTML 1.1 页面 必须用Doctype挑明,再html元素上面 html 4.01 <!DOCTYPE html PUBLIC "-//W3C/ ...

  8. C#使用seleium实现一个自动登录器

    1.http://docs.seleniumhq.org/ 下载seleium包 2.新建一个C#项目,比如控制台,引用seleium包中的dll using System; using System ...

  9. Funsion Charts 学习(二)

    下载FusionCharts 3.1网址为 http://www.onlinedown.net/soft/92224.htm 第一个demo 新建一个文件夹,命名为demo 在文件夹中新建一个两个文件 ...

  10. CGContext

    CGContext又叫图形上下文,相当于一块画布,以堆栈形式存放,只有在当前 context上绘图才有效.iOS有分多种图形上下文,其中UIView自带提供的在drawRect:方法中通过 UIGra ...