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. PCAP文件格式分析(做抓包软件之必备)

    转载源:http://blog.csdn.net/anzijin/article/details/2008333 http://www.ebnd.cn/2009/09/07/file-format-a ...

  2. node.js常用方法

    1.获取真实地址 function getClientIp(req) { return req.headers['x-forwarded-for'] || req.connection.remoteA ...

  3. CF刷刷水题找自信 2

    CF 1114A  Got Any Grapes(葡萄)? 题目意思:给三个人分葡萄,三个人对葡萄的颜色有一些要求,问所准备的三种颜色的葡萄能否满足三人的要求. 解题意思:直接按条件判断即可. #in ...

  4. excel导出功能原型

    本篇博客是记录自己实现的excel导出功能原型,下面我将简单介绍本原型: 这是我自制的窗体,有一个ListView和一个Button(导出)控件. 这是我在网上找到了使用exel需要引用的库. usi ...

  5. 【java】中缀表达式转后缀表达式 java实现

    算法: 中缀表达式转后缀表达式的方法:1.遇到操作数:直接输出(添加到后缀表达式中)2.栈为空时,遇到运算符,直接入栈3.遇到左括号:将其入栈4.遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出 ...

  6. ASP.NET Core 中的 Razor 页面介绍

    标题:ASP.NET Core 中的 Razor 页面介绍 地址:https://docs.microsoft.com/zh-cn/aspnet/core/razor-pages/index?view ...

  7. Struts2(五)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.文件上传 A.单文件上传 <body> <form action="${pageContext.requ ...

  8. 0302借软件工程触IT

         没有不想学好的学生,也没有选择计算机软件专业后不想过能进军IT的行业的.就对于自己情况来说,大学选择计算机商业软件专业学习也有一年多时间了,未接触专业知识前IT是一个高大上的向往,在初学C语 ...

  9. websocket服务器+客户端

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

  10. BZOJ 1965 洗牌(扩展欧几里得)

    容易发现,对于牌堆里第x张牌,在一次洗牌后会变成2*x%(n+1)的位置. 于是问题就变成了求x*2^m%(n+1)=L,x在[1,n]范围内的解. 显然可以用扩展欧几里得求出. # include ...