1.(原文)问题描述

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

For example,
Given s = "the sky is blue",
return "blue is sky the". click to show clarification. 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.

 2.问题翻译

对于一个输入的字符串,逐个词反转这个字符串

例如,

所给的字符串s =  "the sky is blue",
返回 "blue is sky the". 说明: 每个词由什么组成? 由不含空格字符的序列组成每个词 输入的字符串的开头或者结尾能否包含空格? 是的,当然可以。但是在你反转的时候你应该把开头和结尾的字符空格去掉。 如果两个词之间含有多个空格呢? 在反转后的字符串中应该讲他们减少为一个空格。

 3.思路分析

  很显然在拿到字符串的时候首先先进行判断是否为空,如果为空应该原样输出;如果不为空则首先需要处理的情况就是考虑到字符串中每个词之间存在多个空格的情况,

我们需要将这些空格首先变成一个,最后开始处理这个字符串。通过获取字符串数组然后反向输出,在输出的时候拼接空格得到反转后的数组

4.实现过程

Solution.java

package ReverseWords;

public class Solution {

    public String reverseWords(String s) {

    	if(s == null||"".equals(s)){//判断是否为空,为空则直接返回
return "";
}
StringBuffer strBuffer = new StringBuffer();
String []tempArray = s.replaceAll("\\s{1,}", " ").split(" ");//去除多空格情况然后返回字符数组
for(int index = tempArray.length-1;index >= 0;index--){
strBuffer.append(tempArray[index]+" ");//拼接反转数组
}
return strBuffer.toString().trim();
}
}

  SolutionTest.java

package ReverseWords;

public class SolutionTest {

	/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = " a b ";
System.out.println("反转后的字符串是:"+new Solution().reverseWords(str));
} }

 5.感觉考察的东西不多,主要在于细节字符串中空格的处理还有就是字符串相关函数的应用

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

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

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

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

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

  4. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  5. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  6. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

  7. leetcode - [1]Reverse Words in a String

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

  8. LeetCode 345. Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...

  9. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  10. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

随机推荐

  1. Cache基础知识OR1200在ICache一个简短的引论

    以下摘录<步骤吓得核心--软-core处理器的室内设计与分析>一本书 12.1 Cache基本知识 12.1.1 Cache的作用 处理器的设计者通常会声称其设计的处理器一秒钟能做多少次乘 ...

  2. Cocos2d-x在线粒子编辑器

    自由.其效果是非常赞,可以手动调节和.出口可以上网plist档!. 住址:http://particle2dx.com/

  3. mvc5 解析route源码实现自己的route系统

    Asp.net mvc5 解析route源码实现自己的route系统   url route 路由系统的责任是找到匹配的路由,创建路由数据,并将请求分配给一个处理程序. 选择动作是 MVC 的处理程序 ...

  4. Cocos2d-3x:vs2012项目开关android项目需要注意的地方

    http://www.cocoachina.com/bbs/read.php?tid=194668 先依照这个文章导入库到vs项目. 在vs项目的sceen类的里加入 #include "c ...

  5. 使用 CodeIgniter 框架快速开发 PHP 应用(二)

    原文:使用 CodeIgniter 框架快速开发 PHP 应用(二) 二分钟: 建立一个 CodeIgniter 网站用CI建一个网站很容易. 这一章很短,解释了用CI制作网站时发生了些什么,哪些文件 ...

  6. js:深闭包(范围:上)

    /**  * 范围封锁  */ fn1(); //fn1 您可以运行,没有报错,对于由function func_name()这样的写法来定义的函数,永远都会被最先初始化. function fn1( ...

  7. Find a way (BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 BFS搜索  目标地  并记录下来  之后再判断两段路程之和 代码: #include < ...

  8. C语言字符串函数大全

    C语言字符串函数大全 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include ...

  9. hdoj 1226 超级password 【隐图BFS】

    称号:hdoj 1226 超级password 分析:这题属于隐式图搜索,状态不是非常明显,须要自己建立. 事实上搜索说白了就是暴力. 这个题目就是,首先对给出的能够组成的全部的数依次枚举.长度从小到 ...

  10. XMLHttpRequest创建对象

    首先,介绍一下XMLHttpRequest对象,我们都知道Ajax这不是一个简单的技术,但一些技术的融合.XMLHttpRequest这是Ajax中最为核心的技术.假设没有XMLHttpRequest ...