Reverse Words in a String
来源:互联网 发布:java服务器有哪些 编辑:程序博客网 时间:2024/06/16 06:59
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.
public class Solution {
public String reverseWords(String s) {
if(s==null || s.length()==0)
return "";
String[] array = s.split(" ");
StringBuilder sBuilder = new StringBuilder();
for (int i = array.length - 1; i >= 0; --i){
if(!array[i].equals(""))
sBuilder.append(array[i]).append(" ");
}
if(sBuilder.length() == 0)
return "";
else
return sBuilder.substring(0, sBuilder.length()-1);
}
}
public String reverseWords(String s) {
if(s==null || s.length()==0)
return "";
String[] array = s.split(" ");
StringBuilder sBuilder = new StringBuilder();
for (int i = array.length - 1; i >= 0; --i){
if(!array[i].equals(""))
sBuilder.append(array[i]).append(" ");
}
if(sBuilder.length() == 0)
return "";
else
return sBuilder.substring(0, sBuilder.length()-1);
}
}
0 0
- Reverse words in a string.
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- sql plan baselines(计划基线)Oracle 原文
- win32 ipv6 bind 10014问题
- trie树
- PHP控制IP访问
- 关于Spring 国际化 No message found under code 的解决方案
- Reverse Words in a String
- plsql访问oracle数据库64位出错问题解决
- PHP中如何实现首字母数据查询
- 稻壳首页模板index.php的建立
- vi编辑器常用命令
- hihoCoder第二周trie树
- According to TLD or attribute directive in tag file, attribute value does not accept any expressions
- linux套接字通信之recv中的缓存机制的研究
- 通过四种方式让PHP编码变得更轻松