题目: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. SpringBoot 读取properties配置文件 @Value使用 中文乱码问题

    一,idea中配置文件中文乱码问题 使用idea开发,读取properites配置文件 配置: #app 菜单 #没有限制,所有人都可访问的菜单 menu.unlimited=订单审批,现场尽调,合作 ...

  2. Elasticsearch-->Get Started--> Exploring Your Data

    Exploring Your Data Sample Dataset Now that we’ve gotten a glimpse of the basics, let’s try to work ...

  3. 怎样建立你自己的MASM导入库

    by Iczelion (翻译:花心萝卜yqzq@163.net) 9.5.2000 这篇短文是讲述关于建立MASM导入库(import libraries)技巧,我假设你已经知道什么是导入库.在下面 ...

  4. leetcode 04 Median of Two Sorted Arrays

    n1 为 num1的 len n2 为 num2的 len 故中间的数应该是 k = (n1 + n2 + 1) / 2 二分 num1中位置 m1 , 故 num2的位置为m2 必须保证 nums1 ...

  5. MySQL删除数据库时无响应解决办法

    删除远程主机上MySQL中的一个数据库时,远程主机一直在响应,无法正常删除. 这个问题的解决办法如下:在远程主机上登录MySQL,执行show full processlist;观察state和inf ...

  6. P4213 【模板】杜教筛(Sum)(杜教筛)

    根据狄利克雷卷积的性质,可以在低于线性时间复杂度的情况下,求积性函数前缀和 公式 \[ 求\sum_{i=1}^{n}\mu(i) \] 因为\(\mu*I=\epsilon\) 所以设\(h=\mu ...

  7. DAG最小路径点覆盖

    Problem 给出一个有向无环图 (\(DAG\)),求出最少使用其中多少条互不相交的路径覆盖所有点. Solution 若有 \(n\) 个点,对于每个点 \(i\) ,我们将它拆成两个点 \(i ...

  8. 51nod 1689 逛街(优先队列)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1689 题意: 题意: 枚举终点,这样就确定路上的花费,接下来只需要计算进 ...

  9. File类文件的常见操作

    boolean exists() 判断文件或者目录是否存在 boolean isFile()  判断是否是文件 boolean isDirectory() 判断是否是目录 String getPath ...

  10. 【五】php 面向对象

    面向对象 概念:按类进行分类,类是表示彼此之间可能互不相同,但是具有一些共同点的对象集合 多态性:不同的类对同一操作可以有不同的行为 继承:允许我们使用子类在类之间创建层次关系 类 关键字:class ...