题目: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. Print a file's last modified date in Bash

    date -r <filename> #!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t ...

  2. Twenty score

    1.上图中有两个人对读书的看法有较大的不同. There are two people in the cartoon who treat books in completely different w ...

  3. ZJOI 2015 幻想乡战略游戏(动态点分治)

    题意 https://loj.ac/problem/2135 思路 首先要明确一点,答案分布是有单调性的.什么意思呢?假设我们的答案在 \(u\) 节点,\((u,v)\) 之间有一条边且 \(u\) ...

  4. [转载]error while loading shared libraries的解決方法

    转自:https://blog.csdn.net/dumeifang/article/details/2963223 error while loading shared libraries的解決方法 ...

  5. gitlab git

    git网站是进不去的需要加权限才能进去!!!!!!!! 登录进去后 ssh-keygen -t rsa -C "gitlab用户名一般是邮箱" 一路设置好 Use the code ...

  6. ZTree 获取选中的项

    var zTreeOjb = $.fn.zTree.getZTreeObj("zTreeId"); //ztree的Id zTreeId 获取复选框/单选框选中的节点:var ch ...

  7. form提交不刷新,不跳转页面

    利用 iframe 标签 ,后台form处理可以返回void <form action="" method="post" target="nm_ ...

  8. _itemmod_day_limit

    控制玩家每天获得的物品上限 表说明 `comment` 备注 `entry` 物品 `limitCount`获取上限

  9. _itemmod_extra_equipments

    双甲 可以控制获得属性的倍率,及是否可以取回物 `stat_muil`属性倍率(item_template中stat) `enchant_muil`附魔效果中的属性倍率(一些附魔会提升属性,可在些配置 ...

  10. ZJOI-2017 R1游记

    无实力非既得利益的$xrdog$作为一名外卡选手去参加ZJOI2017啦... Day 0: 颓?(细节待填坑..) Day 1: 上午我来到讲课现场发现讲课内容是:搜索专题  QwQ不太清醒的我一下 ...