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. 自定义cursor

    cursor: url('绝对路径/big.cur'),auto;  //通用方式

  2. CCNA 6.9

    page 201 show ip route    Correction(05-4)   Basic configuration of R1:   enable configure terminal ...

  3. Bootstrap非常简单实用的web前端开发框架

    今天无意间用firebug看网站的代码发现了Bootstrap,之前从来没有听说过这个东东,于是对它产生了好奇感,通过百度我了解到了Bootstrap是一款非常简单,强悍,实用,移动设备端优先使用的这 ...

  4. Windows Azure 实操 —— 迁移本地SharePoint服务器到Azure

    博客地址 http://blog.csdn.net/foxdave 注意:如果你是第二代虚拟机,那就别看这个了,老老实实在Azure上重新创建吧,Azure不支持第二代虚拟机. 写在之前,对Azure ...

  5. winform错误提示 :窗口类名无效(Window class name is not valid)

    winfrom 程序在 xp 操作系统上报错提示 窗口类名无效(Window class name is not valid) 解决方法 注释 Program类 里 这句 Application.En ...

  6. iOS多线程之NSThread使用

    iOS中的多线程技术 我们在iOS开发项目过程中,为了解决UI界面操作不被耗时操作阻塞,我们会使用到多线程技术.在iOS开发中,我们主要会用到三种多线程操作技术:NSThread,NSOperatio ...

  7. JVM-class文件完全解析-访问标志

    访问标志 在前面分析了 class文件的魔数,次版本号,主版本号,常量池入口,常量池,那么在常量池结束后,紧接着的两个字节代表访问标志(access_flages).这个标志用于识别一些类或者接口层次 ...

  8. 学会使用Ogitor

    这几天在用Ogre读取Ogitor的场景,遇到了不少问题,在网上也找不到详细的说明,虽然读取Ogitor的场景对很多人来说太简单了,但对一些新手来说就有点难了,我刚开始就觉得是无从下手,因此简单的描述 ...

  9. FreebuF黑客专访系列之吴翰清(刺):接下来几年,有两样东西必定会火

        注:吴翰清——中国网络安全圈最具影响力的人物之一.西安交大少年班毕业,大学期间创办民间组织幻影,阿里巴巴集团最年轻的高级安全专家,创新工场安全宝任联合副总裁,热门公众微信“道哥的黑板报”幕后作 ...

  10. SVG 2D入门5 - 颜色的表示

    SVG和canvas中是一样的,都是使用标准的HTML/CSS中的颜色表示方法,这些颜色都可以用于fill和stroke属性.基本有下面这些定义颜色的方式:1. 颜色名字: 直接使用颜色名字red, ...