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 ...
随机推荐
- js分家效应
(原创文章,转载请注明出处) 有继承,那么就有分家.让我们看以下例子. var parents = function(){ } parents.prototype.money = 200; var c ...
- js自定义方法名
自定义方法名: <script language="javascript" type="text/javascript">window.onload ...
- 开通博客第一天 (先发一些android(java)常见异常信息
常见异常: java.lang.AbstractMethodError抽象方法错误.当应用试图调用抽象方法时抛出. java.lang.AssertionError断言错.用来指示一个断言失败的情况. ...
- js获取标签的几种方式
一:id获取(全部浏览器兼容) document.getElementById(""); <body> <div id="box">&l ...
- 使用charles proxy for Mac来抓取手机App的网络包
之前做Web项目的时候,经常会使用Fiddler(Windows下).Charles Proxy(Mac下)来抓包,调试一些东西:现在搞Android App开发,有时候也需要分析手机App的网络请求 ...
- Linux命令:scp命令(文件上传和下载)
#本地下载远端文件 并且重命名(从本地机器下载远端) scp webmaster@10.10.65.103:/ROOT/logs/tomcate.log /home/dajie/mywork/newn ...
- C /CLI思辨录[阅读记录]
C /CLI思辨录之拷贝构造函数(避免多个实例在相同的堆对象的错误处理) http://west263.com/info/html/chengxusheji/C-C--/20080224/9247.h ...
- phpcms v9 get调用标签整理
常见的phpcms标签调用 1.调用本系统单条数据(调用ID为1的信息,标题长度不超过25个汉字,显示更新日期): "select * from phpcms_content where ...
- 快速替换图片的组合-AE-样片!
模板下载网址:http://pan.baidu.com/s/1hqCbErM
- Windows Phone 8 SQL Server CE 数据库
员工信息表 EmployeeTable.cs using System.Data.Linq.Mapping; using System.ComponentModel; namespace SQLSer ...