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. mahout源码KMeansDriver分析之五CIMapper初探

    接着上篇,继续分析代码.下面就到了MR的循环了,这里MR应该算是比较好理解的,重点是退出循环的条件设置,即如何判断前后两次中心点误差小于给定阈值. 首先,while循环: while (iterati ...

  2. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  3. linux 字符界面浏览器 w3m(转)

    最近找到了几个linux终端下使用的浏览器,主要用来测试本机web服务器是否搭建成功.因为我们一般是用ssh客户端连接linux的,所以很需要一个字符界面的浏览器.找了几个显示都不理想,只有w3m用起 ...

  4. Hadoop0.20.2 Bloom filter应用演示样例

    1. 简单介绍 參见<Hadoop in Action>P102 以及 <Hadoop实战(第2版)>(陆嘉恒)P69 2. 案例 网上大部分的说明不过依照<Hadoop ...

  5. 初识 Cloudera Impala

    Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统尽管也提供了SQL语义,但因为Hive底层 ...

  6. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  7. OCP读书笔记(21) - 题库(ExamA)

    Administer ASM disk groupsBack up the recovery catalogConfigure backup settingsConfigure, Monitor Fl ...

  8. 解决opengl计算顶点的法线问题

    因为需要的论文,最近开始学习OpenGL.由于刚入门的初学者有这么总会遇到很多问题,. 这些天,好不容易才OpenGL个问题弄明确了. 几点迷惑: 在网上百度.发现非常多求平面法向量的介绍以及程序.后 ...

  9. js地址下拉列表中全职工作

    /******************************************************************* *输出全国各省辖市下拉列表项writeCitys() *输出企 ...

  10. 霍夫曼(最优二叉树)和Java达到

    一.定义 一些定义: 节点之间的路径长度:在从节点树中的一个节点也经历分公司,这构成的两个节点之间的路径分支的数目后这就是所谓的路径长度 的路径长度:从树的根节点到树中每一结点的路径长度之和. 在结点 ...