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. Python:字符串操作总结

    所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最小值最大值)对字符串同样适用,且字符串是不可变的. 一.字符串格式化 转换说明符 [注]: 这些项的顺序至关重要 (1)%字符:标记转换 ...

  2. web.config详解(转载)

    该文为转载 原文地址:http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html 花了点时间整理了一下ASP.NET Web.c ...

  3. 结对编程——paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...

  4. websocket服务器+客户端

    <?php $demo = new ws('192.168.90.47',12345); $demo->run(); class ws { //当前服务端主连接 private $curr ...

  5. 第75天:jQuery中DOM操作

    一.基础操作 1.html() 使用html()方法读取或者设置元素的innerHTML. 就是相当于javascript里头的innerHTML. 2.text() 使用text()方法读取或者设置 ...

  6. 百度editor编辑器添加新字体

    Ueditor本身自带11种字体,添加如下: 1.找到文件 ueditor/lang/zh-cn/zh-cn.js ,添加字体 'fontfamily': {        'songti': '宋体 ...

  7. SQL入门之集合操作

    尽管可以在与数据库交互时一次只处理一行数据,但实际上关系数据库通常处理的都是数据的集合.在数学上常用的集合操作为:并(union),交(intersect),差(except).对于集合运算必须满足下 ...

  8. BZOJ 1853 幸运数字(容斥原理+dfs)

    题意:求闭区间内能被6和8组成的数字整除的数目.n<=1e11. 我们可以预处理出这些6和8组成的数字,大概2500个,然后排除一些如88,66的情况.这样大概还剩下1000个. 转化为[0,r ...

  9. BZOJ 1212 L语言(DP+字典树)

    求能被理解的最长前缀. 很显然的dp.令dp[i]=true,表示前缀i能理解.否则不能理解.那么dp[i+len]=dp[i]=true,当s[len]能匹配str[i,i+len]. 由于模式串长 ...

  10. BZOJ4985 评分(二分答案+树形dp)

    首先二分答案简化一下问题,现在只有0和1了,要求最后剩下的是1.再简化一下考虑没有已固定的位置怎么做.考虑每个位置由其合并到的位置连边,显然这样形成了一棵三叉树.设f[i]为使得某位置为1其子树至少要 ...