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. 黑马程序员——C语言基础 scanf函数 基本运算 三目运算符

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (一下内容是对黑马苹果入学视频的个人知识点总结) (一)scanf函数 1>  简单介绍一下scanf函数   这是在 ...

  2. HTML特殊符号对应代码

    HTML中的特殊符号所对应代码,收藏使用 符号 HTML 符号 HTML     & & < < > > ⁄ ⁄ " " ¸ ¸ ° ° ½ ...

  3. cvWaitKey

    OpenCV中的一个函数 函数原型为: C++: int waitKey(int delay=0) Python: cv2.waitKey([delay]) → retval C: int cvWai ...

  4. SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)

    博客地址 http://blog.csdn.net/foxdave 本篇讲解一个有些新颖的SharePoint实例应用,给甲方做过项目的都有过体会,数据太多了,客户有Excel,要求实现批量导入. 效 ...

  5. Activity(活动)-初讲

    是一种可以包含用户界面的组件,主要用于和用户进行交互. 上一次我们的MainActivity.java 是ADT帮我们自动创建的.手动创建Activity可以加深我们的理解和记忆,于是我们先自己手动创 ...

  6. 《Java实验四》

    //实验4--附录一代码 public class PassValueTest { //静态代码块,类一加载就执行的部分. //所以运行这个程序会输出 class loding static { Sy ...

  7. ACM - 动态规划专题 题目整理

    CodeForces 429B  Working out 预处理出从四个顶点到某个位置的最大权值,再枚举相遇点,相遇的时候只有两种情况,取最优解即可. #include<iostream> ...

  8. ALAssets的两种用法

    一: ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *result, NSUInteger index, BOOL *st ...

  9. postgreSQL9.1忘记postgres用户密码怎么办

    在网络上找了一篇文章http://www.linuxidc.com/Linux/2010-04/25232.htm,如下: Ubuntu 9.10下PostgreSQL 8.4忘记密码的解决方法 Ub ...

  10. Nginx 安装编译配置

    ./configure --prefix=/usr/local/nginx-1.8.0 --with-http_ssl_module --with-http_spdy_module --with-ht ...