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. loadrunner socket协议问题归纳(4)---buffer接收变长和定长的数据

    测试场景:聊天系统 用户登录后,要先向服务器发送用户名,然后可以发送聊天信息,同时也可以接受聊天信息. 如果接受的字符为定长时,可以设定接受长度.recv buf2 66 #include " ...

  2. Unicode 和 UTF-8 有何区别

    作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有,转载请联系作者获得授权. ========== ...

  3. 使用Kettle导出excel

    1.开发背景 在web项目中,经常会需要查询数据导出excel,以前比较常见的就是用poi.使用poi的时候也有两种方式,一种就是直接将集合一次性导出为excel,还有一种是分批次追加的方式适合数据量 ...

  4. Mac 常用快捷键整理

    Mac下常用的快捷键: Command+W 将当前窗口关闭(可以关闭Safari标签栏,很实用) Command+Option+M 将所有窗口最小化 Command+Q 关闭当前应用程序(相当于Doc ...

  5. 新浪 ip 地址库

    API地址:http://int.dpool.sina.com.cn/iplookup/iplookup.php 帮助 1 2 3 4 5 6 7 8 function get_location($i ...

  6. Windows配置java运行环境的步骤

    jdk不同版本下载地址:http://www.oracle.com/technetwork/java/javase/archive-139210.html 1.下载你适合你电脑的jdk版本,链接如上, ...

  7. urllib2 request 模拟伪装浏览器

    直接上代码吧 # -*- coding:utf-8 -*- import urllib2 import random url = "http://www.baidu.com/" # ...

  8. 【Python】python 反射机制在实际的应用场景讲解

    剖析python语言中 "反射" 机制的本质和实际应用场景一. 前言 def s1(): print("s1是这个函数的名字!") s = "s1&q ...

  9. 树状数组模板(pascal) 洛谷P3374 【模板】树状数组1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  10. 【刷题】LOJ 6041 「雅礼集训 2017 Day7」事情的相似度

    题目描述 人的一生不仅要靠自我奋斗,还要考虑到历史的行程. 历史的行程可以抽象成一个 01 串,作为一个年纪比较大的人,你希望从历史的行程中获得一些姿势. 你发现在历史的不同时刻,不断的有相同的事情发 ...