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.

倒序字符串。

注意使用BufferString,因为如果使用String的话,会多次建立对象,会很慢。

public class Solution {
public String reverseWords(String s) {
int len = s.length();
if( len == 0)
return s;
StringBuffer result = new StringBuffer() ;
int end = len-1,start = 0;
while( end >= 0 && s.charAt(end) == ' ')
end--;
if( end == -1)
return result.toString();
while( start < len && s.charAt(start) == ' ')
start++;
int pos = end+1;
for( int i = end ; i >= start ; i-- ){
if( s.charAt(i) == ' ') {
result.append(s.substring(i+1,pos));
result.append(" ");
while (i < end && s.charAt(i) == ' ')
i--;
i++;
pos = i; }
}
result.append(s.substring(start,pos));
return result.toString();
}
}

leetcode 151. Reverse Words in a String --------- java的更多相关文章

  1. [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& ...

  2. Java for 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 ...

  3. [leetcode]151. Reverse Words in a String翻转给定字符串中的单词

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

  4. LeetCode 151 reverse word in a string

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  5. Leetcode#151 Reverse Words in a String

    原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 151. Reverse Words in a String(java 注意细节处理)

    题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...

随机推荐

  1. convert2Mp4 code snippet

    #pragma mark - helper - (NSURL *)convert2Mp4:(NSURL *)movUrl { NSURL *mp4Url = nil; AVURLAsset *avAs ...

  2. string.format

    string.Format("{0:#,0}", c.num), //千分号,有小数就保留2位小数 string.Format("{0:N2}", c.amou ...

  3. 标准库源码--wsgi服务器

    功能模块化带来可自由组装的便利: 使用python的mixin特性装配 class ThreadHTTPServer(ThreadingMixin, HTTPServer): pass 上面表示1个请 ...

  4. JS小问题总结

    1. 超链接中href=#与href=javascript:void(0) 的区别  #包含了一个位置信息.默认的锚是#top 也就是网页的上端:而javascript:void(0)   仅仅表示一 ...

  5. Volatile vs VolatileRead/Write?

    You should never use Thread.VolatileRead/Write(). It was a design mistake in .NET 1.1, it uses a ful ...

  6. MongoDB数据访问[C#]附源码下载(查询增删改) 转载

    安装完MongoDBhttp://localhost:28017/监测是否成功! vs 2008 C# MongoDB 源代码下载地址:http://download.csdn.net/source/ ...

  7. jquery的is用法

    JQuery 中 is(':visible') 解析及用法 javascript代码$(document).ready(function() {           $('#faq').find('d ...

  8. 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解

    本文主要讲的是<天龙八部>游戏中水面(TerrainLiquid)的具体实现,使用C++,Ogre1.6. 天龙的水面做的比较简单,虽然没有倒影,但动态纹理+深度图做出的效果还行,看着不是 ...

  9. powerdesigner的学习

    1  powerdesigner的层次 使用过eclipse作为java开发的都知道,workspace是顶级目录,一次只能打开一个,project是项目,位于workspace中,一个workspa ...

  10. php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动

    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动 每个人机器不一样,我手头是个air book,查了一下现在最好在mac下,用mamp, mamp百科介绍 , ...