题目:reverse words in a string

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

解析:将字符串中的单词逆序输出

借助一个堆栈,从前向后遍历字符串,遇到空,跳过,直到非空字符,拼接word,等再次遇到空时,得到一个word,加入堆栈,

以此类推,直到遍历到s的最后一个字符为止。

最后,将堆栈中的word依次输出

java编码:

方法一:

public String reverseWords(String s) {
if(s == null || s.length() == 0)
return s;
int index = 0; //the pointer to traverse
int len = s.length();
Stack<String> stack = new Stack<String>(); //堆栈,先进后出,顺序存入单词,逆序输出
StringBuilder sBuilder = new StringBuilder(); //记录每一个单词
char[] characters = s.toCharArray();
while(index < len){ //遍历字符串
for(;index < len && characters[index] == ' '; ++index);//跳过空字符
for(;index < len && characters[index] != ' '; ++index){//拼接word
sBuilder.append(characters[index]);
}
if(sBuilder.length() > 0){//将有效的word压入堆栈
stack.push(sBuilder.toString());
sBuilder.delete(0,sBuilder.length());//清空stringbuilder
}
} sBuilder.delete(0,sBuilder.length());//清空stringbuilder while(!stack.isEmpty()){//输出,空格分隔
sBuilder.append(stack.pop() + " ");
}
return sBuilder.toString().trim();
}

方法二:

正则表达式,\s表示空格,+表示至少一个空格,这样就可以将多个空格分隔的word提取出来了

public String reverseWords(String s) {
String[] words = s.split("\\s+"); // regular expression
StringBuffer sb = new StringBuffer(); for(int i = words.length - 1; i >= 0; --i){
sb.append(words[i] + " ");
} return sb.toString().trim(); //trim是去除字符串的首尾空格
}

151. Reverse Words in a String(java 注意细节处理)的更多相关文章

  1. leetcode 151. Reverse Words in a String --------- java

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

  2. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

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

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

  4. 【刷题-LeetCode】151 Reverse Words in a String

    Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...

  5. Java for LeetCode 151 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】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  7. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  8. (String)151. Reverse Words in a String

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

  9. 151. Reverse Words in a String

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

随机推荐

  1. CF1131D tarjan,拓扑

    题目链接 541div2 http://codeforces.com/contest/1131/problem/D 思路 给出n序列和m序列的相对大小关系 构造出最大值最小的序列 缩点+拓扑 小的向大 ...

  2. List of 3rd Party .NET UI & Reporting Components

    https://www.codeproject.com/Reference/788434/List-of-rd-Party-NET-UI-Reporting-Components Introducti ...

  3. springBoot 学习(总)

    springBoot有个中文文档网址,应该是持续更新的. 别的资料 http://tengj.top/2017/02/26/springboot1/     http://412887952-qq-c ...

  4. 论文笔记:Attention Is All You Need

    Attention Is All You Need 2018-04-17 10:35:25  Paper:http://papers.nips.cc/paper/7181-attention-is-a ...

  5. HTTP 返回状态码说明

    HTTP 返回状态码一.1xx - 信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应. • 100 - 继续. • 101 - 切换协议. 二.2xx ...

  6. 【Luogu P2764】最小路径覆盖问题

    网络流 \(24\) 题之一. Problem Description 给出一个 \(n\) 个点 \(m\) 条边的 \(DAG\) ,求最小路径点覆盖,并输出路径选择方案. Input Forma ...

  7. VR外包团队:魔幻的三星GearVR

    如果问大大菌,移动端什么产品最好,大大菌一定会和你说,是gearvr,对于已经购买了三星2015旗舰手机.又对VR技术感到好奇的用户,建议花99美元买一台Gear VR,绝对会让你物有所值. 在短短的 ...

  8. 转 lightmap

    小记一下用法与问题,时更 surface shader就不用操心了,自带lightmap计算 主要是vertex fragment shader部分 Unity5 bake light map有三种情 ...

  9. 【一】jquery之subline编辑器插件安装

    1.地址下载:https://pan.baidu.com/share/link?shareid=552312&uk=151954025 2.打开Sublime, 选择 Prefreences  ...

  10. lombok-@Accessors注解

    @Accessors 有3个选项:如图默认是false 1.当fluent = true时 2.当fluent = true时