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 ...
随机推荐
- Android.9图片评论(一个)
什么是.9图片 至于什么是.9图片这里就简单提一下,即图片后缀名前有.9的图片,如pic.9.png.pic1.9.jgp,诸如此类的图片就称为.9图片. .9图片的作用 ①.9图片的作用是在图片拉伸 ...
- 高通公司 MSM8K GPT异常原因分析无法开机的问题
问题分析过程如下面: 一. MSM8916台gpt概率问题:采用QPST emmc software download下载软件工具后,无法开机.例如下面的附图: log分析是userdata分区未成功 ...
- 网站的SEO以及它和站长工具的之间秘密(转)
博客迁移没有注意 URL 地址的变化,导致百度和 google 这两只爬虫引擎短时间内找不到路.近段时间研究了下国内最大搜索引擎百度和国际最大搜索引擎google的站长工具,说下感受. 百度的站长工具 ...
- (大数据工程师学习路径)第一步 Linux 基础入门----目录结构及文件基本操作
Linux 目录结构及文件基本操作 介绍 1.Linux 的文件组织目录结构. 2.相对路径和绝对路径. 3.对文件的移动.复制.重命名.编辑等操作. 一.Linux 目录结构 在讲 Linux 目录 ...
- IP地址解析
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- uva 1556 - Disk Tree(特里)
题目连接:uva 1556 - Disk Tree 题目大意:给出N个文件夹关系,然后依照字典序输出整个文件文件夹. 解题思路:以每一个文件夹名作为字符建立一个字典树就可以,每一个节点的关系能够用ma ...
- C/C++基础笔试题1.1.2(十问解决方案运营商)
他写在博客上不负责任,有人踩在上面,原因一点点失去了它. 其实纯常见问题.特别糟糕,由于鲍文看起来很大的优势就是想更直接看到的东西. 好了.返回整个标题. 1.你脑海中的运算符有哪些.按优先级排个序? ...
- HDU 4085 Steiner树
主题链接:pid=4085">http://acm.hdu.edu.cn/showproblem.php? pid=4085 由于这题专门花一晚上学习斯坦纳树.找到比較好的学习资料,链 ...
- Microservice架构模
Microservice架构模 在2014年,Sam Newman,Martin Fowler在ThoughtWorks的一位同事,出版了一本新书<Building Microservices& ...
- Swift——(三)Swift神奇的下划线
在Swift在.有许多神奇的下划线,在这里,我们将看到神奇的汇总,我希望能够帮助其他很多学习Swift朋友. @Author: twlkyao转载或者引用请保留此行. 1.格式化数字字面量 通 ...