leetcode 151. Reverse Words in a String --------- java
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的更多相关文章
- [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& ...
- 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 ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- 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 ...
- Leetcode#151 Reverse Words in a String
原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- error: unknown field 'ioctl' specified in initializer
error message: 原因: 从2.6.36开始,file_operations结构发生了重大变化 具体看 xx../include/linux/fs.h定义: 取消了原先的 int (*i ...
- 字符串常量演示Demo
public class StringDemo { public static void main(String[] args) { // TODO Auto-generated method stu ...
- (转)SoftReference
本文介绍对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说, ...
- Cookie 与Session 的区别(转载)
原地址: http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html 两个都可以用来存私密的东西,同样也都有有效期的说法. 区别在 ...
- (转)js函数参数设置默认值
原文:http://www.cnblogs.com/RightDear/archive/2013/06/26/3156652.html js函数参数设置默认值 php有个很方便的用法是在定义函数时 ...
- Python 3.x print 小结
Python 思想: “一切都是对象!” input("Press Enter") 就可以让程序运行完后停一下 输出的 print 函数总结: 1. 字符串和数值类型可以直接输出 ...
- php大力力 [005节] php大力力简单计算器001
2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...
- 配置VS2010具有代码提示功能
Visual Assist X是一款非常好的Microsoft Visual Studio插件,可以支持Microsoft Visual Studio 2003,Microsoft Visual St ...
- Testing the CATCHER_DP
Description A military contractor for the Department of Defense has just completed a series of preli ...
- 后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)
using IBatisNet.Common.Exceptions; using IBatisNet.DataAccess; using IBatisNet.DataAccess.DaoSession ...