package com.lw.leet1;

 import java.util.Stack;

 /**
* @ClassName:Solution
* @Description:
* Reverse Words in a String
* Total Accepted: 26194 Total Submissions: 187094 My Submissions
* Given an input string, reverse the string word by word.
*
* For example
* Given s = "the sky is blue"
* return "blue is sky the".
*
* Clarification:
* What constitutes a word?
* A sequence of non-space characters constitutes a word.
* Could the input string contain leading or trailing spaces?
* Yes. However, your reversed string should not contain leading or trailing spaces.
* How about multiple spaces between two words?
* Reduce them to a single space in the reversed string.
*
* @Author LiuWei
* @Date 2014年8月15日下午7:48:48
* @Mail nashiyue1314@163.com
*/
public class Solution { public String reverseWords(String word){
Stack<String> sstack = new Stack<String>();
int flag = 0;
for(int i= 0; i<word.length(); i++){
while(i<word.length() && word.charAt(i)==' '){
i++;
}
flag = i;
while(i<word.length() && word.charAt(i)!=' '){
i++;
}
if(flag != i){
sstack.push(word.substring(flag, i));
}
}
String res = "";
while(!sstack.isEmpty()){
res += sstack.pop()+" ";
}
// The input string which is made up of space
if(res.length()==0){
return "";
}
return res.substring(0, res.length()-1);
} public String reverseWords2(String word){
String res ="";
int flag = 0;
for(int i= 0; i<word.length(); i++){
while(i<word.length() && word.charAt(i)==' '){
i++;
}
flag = i;
while(i<word.length() && word.charAt(i)!=' '){
i++;
}
if(flag != i){
res = word.substring(flag, i)+" "+res;
}
}
// The input string which is made up of space
if(res.length()==0){
return "";
}
return res.substring(0, res.length()-1);
} public static void main(String[] args){
Solution s = new Solution();
System.out.println(s.reverseWords2(" hello world "));
}
}

LeetCode-Reverse Words in a String[AC源码]的更多相关文章

  1. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  2. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  3. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  4. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  5. [LeetCode] Reverse Words in a String 翻转字符串中的单词

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

  6. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  7. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  8. java.lang.String 类源码解读

    String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...

  9. 翻String.Format源码发现的新东西:StringBuilderCache

    起因: 记不清楚今天是为毛点想F12看String.Format的实现源码了,反正就看到了下图的鸟东西: 瞬间石化有没有,StringBuilder还能这么获取? 研究StringBuilderCac ...

随机推荐

  1. Java 更改日期格式

    import java.util.*; import java.text.*; public class TestDateFormat { public static void main(String ...

  2. Objective - C 之类目

    一.类目(category):为已有的类(可以是系统类,也可以是自定义类)添加公有的新的方法: 例如:为系统已有的NSString类添加一个比较字符串大小的方法 1.创建过程: 2.NSString ...

  3. 2nd 历年学生作品评论(3部)

    历年学生作品评论(3部) 1.基于GUI的图书管理系统 利用NABCD模型进行竞争性需求分析:http://www.cnblogs.com/chitty/p/4546876.html 测试说明书: h ...

  4. c 读取文本

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define max 10 #define ...

  5. android studio 运行太慢了

    Android Studio每次升级/安装 Android Studio 之后最好都修改一下这个参数:到 Android Studio 安装目录,找到 bin/studio(64?).vmoption ...

  6. solr源码分析之searchComponent

    上文solr源码分析之数据导入DataImporter追溯中提到了solr的工作流程,其核心是各种handler. handler定义了各种search Component, @Override pu ...

  7. C#非泛型集合类与泛型集合类的区别 (转)

    来自:http://blog.csdn.net/jiayanhui2877/article/details/7623845 C# 泛型集合之非泛型集合类与泛型集合类的对应: ArrayList对应Li ...

  8. Xmind8破解,以及相关的流程和破解包

    一.下载XMindCrack.jar文件:(传的貌似被屏蔽了:如果需要请留下邮箱,抽空会发给你) 百度云 ,里面破解文件,安装包都给了,但Xmind安装包不一定是最新的,有需求的可自行去官网下载 . ...

  9. robot framework 安装

    一.安装 Python 2.7 pip 和 setuptools (Python 的套件管理程式,最新版的Python 2.7.13已包含) Robot Framework (此工具本身) wxPyt ...

  10. 转:解决Python中文编码问题

    Python 文本挖掘:解决Python中文编码问题 转于:http://rzcoding.blog.163.com/blog/static/2222810172013101785738166/   ...