您的位置:首页 > 编程语言 > Java开发

leetcode:151Reverse Words in a String java实现

2015-09-17 19:15 453 查看
Given an input string, reverse the string word by word.

For example,

Given s = "
the sky is blue
",

return "
blue is sky the
".
题目分析:太简单了,要注意一点 split可能会返回空串
<pre name="code" class="java"> public String reverseWords(String s) {
		  if(s.length()==0) return s;
		  String[] array=s.split(" ");
		  if(array.length==0) return new String();
		  StringBuffer sb=new StringBuffer();
		  for(int i=array.length-1;i>=0;i--)
		  {
			  if(array[i].length()==0) continue;//" 1"分割会出现空串和1
			  sb.append(array[i]+" ");
		  }
		  return sb.toString().substring(0, sb.length()-1);

	    }




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: