LeetCode之Reverse Words in a String
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的更多相关文章
- [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& ...
- [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 ...
- [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 ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 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 ...
- 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 ...
- 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 ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- 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 ...
随机推荐
- CentOS7 安装spark集群
Spark版本 1.6.0 Scala版本 2.11.7 Zookeeper版本 3.4.7 配置虚拟机 3台虚拟机,sm,sd1,sd2 1. 关闭防火墙 systemctl stop firewa ...
- 最受欢迎telnet
点击开关在模拟器,它相当于实PC经由控制线连接真实开关 由于我使用telnet远程登录到交换机的话.因为telnet工作在TCP/IP模型的应用层,现在让应用层通信的话,我首先要保证网络层通信,通信, ...
- JavaEE(18) - JPA原生SQL查询和存储过程
1. 使用原生SQL执行查询 2. 映射原生SQL查询的结果集 3. 使用原生SQL查询执行实体查询 4. 命名原生SQL查询 5. 在JPQL查询中调用存储过程
- SQL Server的备份
原文:SQL Server的备份 0.参考文献 1.恢复模式 SQL Server 备份和还原操作发生在数据库的恢复模式的上下文中. 恢复模式旨在控制事务日志维护. “恢复模式”是一种数据库属性,它控 ...
- UML对象图和包图
UML九已经介绍过的基本图,然后,我们再来看看对象图和包图. 一.对象图 谈到对象.我们不得不说一下对象.对象(Object)是对象类的实例(Instance),用于模型化特定的实体.对象是唯一的. ...
- SSIS从理论到实战,再到应用(2)----SSIS包的控制流
原文:SSIS从理论到实战,再到应用(2)----SSIS包的控制流 前文回顾: SSIS从理论到实战,再到应用(1)----创建自己的第一个包 上次说到创建了自己的第一个包,完成了简单的数据从数据库 ...
- Hadoop 2.2.0 HA构造
在这篇文章中<Ubuntu和CentOS分布式配置Hadoop-2.2.0>介绍hadoop 2.2.0最主要的配置.hadoop 2.2.0中提供了HA的功能,本文在前文的基础上介绍ha ...
- HDU 1203 I NEED A OFFER!(dp)
Problem Description Speakless很长时间,我想出国.现在,他已经完成了所有需要的检查.准备好所有要准备的材料,于是,便须要去申请学校了.要申请国外的不论什么大学.你都要交纳一 ...
- Tyvj P1015 公路骑 (DP)
叙述性说明 Description 一个特殊的单行道都在每公里公交车站.们乘坐汽车的公里使来付费. 比如例子的第一行就是一个费用的单子. 没有一辆车子行驶超过10公里.一个顾客打算行驶n公里(1 ...
- kobject_create_and_add
本文来源于源linux 3.14.3版本号/lib/kobject.c文件 /** * kobject_create_and_add - 动态地创建kobject结构和寄存器sysfs * * ...