Reverse Words in a String——LeetCode
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
- 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.
题目大意:给一个String,以空格分隔,以单词为单位反转这个String。
解题思路:用java自带的split,拆成数组,然后组合。。。
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return s;
}
String[] res = s.split(" ");
if (res.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = res.length - 1; i >= 0; i--) {
if ("".equals(res[i])) {
continue;
}
sb.append(res[i]).append(" ");
}
return sb.substring(0, sb.length() - 1);
}
Reverse Words in a String——LeetCode的更多相关文章
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- Reverse Words in a String leetcode
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
- Reverse Words in a String | LeetCode OJ | C++
我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...
- [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 ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- 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 ...
随机推荐
- hadoop集群环境搭建之安装配置hadoop集群
在安装hadoop集群之前,需要先进行zookeeper的安装,请参照hadoop集群环境搭建之zookeeper集群的安装部署 1 将hadoop安装包解压到 /itcast/ (如果没有这个目录 ...
- Oracle高版本导出dmp导入Oracle低版本报错:"不是有效的导出文件、头部验证失败"解决方法
从Oracle高版本中导出dmp,然后导入到Oracle低版本时会报错:"不是有效的导出文件.头部验证失败",解决方法: 方法一:下载软件:AlxcTools,打开后选择要修改的文 ...
- NC V6 nchome文件目录及其作用介绍
NC V6发布一段时间了,各个NC6.0 nchome文件夹下各个子文件夹内容和作用 ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsy ...
- C# - string 转为 DateTime(自定义)
上代码: string dt = " 1 11 1961"; DateTime day; System.Globalization.DateTimeFormatInfo dtFor ...
- Tomcat- java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet
在MyEclipse中启动Tomcat的时候报错: java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServ ...
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- youphp学习整理
<?php //后台公共模块 // _list 数据显示 // add 添加/编辑 视图 // insert 添加处理函数 // edit 添加/编辑 视图 // update 更新处理函数 / ...
- metalink下载补丁包
以下截图 截取自 某 升级包中携带的 readme文档 把以上图片转换为 文字 Download and Install Patch Updates Refer to the My Oracle Su ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- alsa utils工具使用
1.amixer用于控制设置 amixer [-c card] [cmd] ./amixer contents ./amixer cset ./amixer cget 2. aplay ./aplay ...