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. 20135313_exp5

    课程:Java程序与设计     班级:1353 姓 名:吴子怡  学号:20135313 小组成员: 20135113肖昱 成绩:             指导教师:娄嘉鹏       实验日期:2 ...

  2. PHP 函数总结

    感觉对函数了解的不够深,从头到尾梳理一遍(更新中....) 1,class_exists(),interface_exists(),method_exists(),get_class(),get_pa ...

  3. WebService(三)

    JAX-WS简单使用示例: 1.服务端 package com.rong.service; import javax.jws.WebMethod; import javax.jws.WebParam; ...

  4. HTML&CSS实体

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. Markdown使用github风格时报TLS错误解决办法

    https://docs.microsoft.com/en-us/officeonlineserver/enable-tls-1-1-and-tls-1-2-support-in-office-onl ...

  6. Problem D - Non-boring sequences——Contest1004 - National Day Training Contest -- Day3

    今天比赛的时候做的一个坑题.深坑啊. 题目意思是给你一个有n个数的数字序列.要你判断对于这个序列是都满足任意一个子序列都至少含有一个只出现一次的数字. 看完题目后没什么思路,一直以为要用线段树,每次删 ...

  7. week1day01 认识python 变量 数据类型 条件if语句

    1.什么是python? Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年.像Pe ...

  8. grub引导启动 win10 Ubantu 凤凰OS 三系统

    在Ubantu OS下,用文件管理器打开系统磁盘下的 boot文件夹,然后用管理员身份打开grub文件夹,然后打开grub.cfg(用记事本打开) 4. 在grub.cfg文件里面找到下一段内容(比较 ...

  9. [SCOI2016]幸运数字 线性基

    题面 题面 题解 题面意思非常明确:求树上一条链的最大异或和. 我们用倍增的思想. 将这条链分成2部分:x ---> lca , lca ---> y 分别求出这2个部分的线性基,然后合并 ...

  10. 【JQuery】css操作

    一.前言         接着上一章的内容,继续JQuery的学习 二.内容 css 设置或返回匹配元素的样式属性 $(selector).css(css-property-name) $(selec ...