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. 集成支付宝SDK遇到的坑

    一.首先我先把集成过程说一下.小编想说的话:支付宝是我做支付中觉得坑最多的一个,各种编译不过,各种出问题. 废话不多说,进入主题:1.首先当前是下载官方SDK啦,当前你也可以通过cocopods进行导 ...

  2. 格而知之15:我所理解的Block(1)

    1.Block 本质上是一个struct结构体,在这个结构体中,最重要的成员是一个函数(当然除函数外还有其他重要的成员). 2.在开始解析Block之前,首先来回顾一下Block的格式.Block相关 ...

  3. java BingInteger生成2进制String循环移位时长度自动缩减

    最近在做文本处理,使用MD5 生成一段文字的MD5哈希长度为32位也即128个0-1序列. 由于需要对这个MD5值进行循环移位,显然普通的  int 是不行的,所以使用 BigInteger.但是在使 ...

  4. 8. 冒泡法排序和快速排序(基于openCV)

    一.前言 主要讲述冒泡法排序和快速排序的基本流程,并给出代码实现,亲测可用. 二.冒泡法排序 冒泡法排序主要是将相邻两个值比较,把小的向前冒泡,大的向后沉淀,时间复杂度为O(n2).主要思想如下: 分 ...

  5. Redis 安装教程 (Windows 2.6.13 稳定版)

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  6. gdalwarp:变形工具

    1 gdalwarp:变形工具.包括投影.拼接.及相关的变形功能.此工具功能强大,但效率不高,使用时注意 gdalwarp [--help-general] [--formats]     [-s_s ...

  7. C#中string.Empty和""、null的区别

    string.Empty是string类的一个静态常量,而""则表示一个空字符串. string是一种特殊的引用类型,它的null值则表示没有分配内存. 使用ILSpy反编译Str ...

  8. javascript运动功能-分享到

    <script> //窗体载入,为div控件绑定事件 window.onload = function () { var div1 = document.getElementById('d ...

  9. UML学习-活动图创建

    活动图(Activity Diagram)可以实现对系统动态行为的建模,主要是将用例细化,即用例内部的细节可以以活动图的方式描述.活动图描述活动的顺序,主要表活动之间的控制流,是内部处理驱动的流程,在 ...

  10. 1230.2——iOS准备(阅读开发者文档时的笔记)

    1.程序启动的过程    .在桌面找到相应的应用的图标 点击图标    .main函数 UIApplication类Every app has exactly one instance of UIAp ...