151. Reverse Words in a String(java 注意细节处理)
题目: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 注意细节处理)的更多相关文章
- 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& ...
- 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 ...
- [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& ...
- 【刷题-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: ...
- 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 ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- (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& ...
- 151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
随机推荐
- 【原理、注意点】Quartz的原理和需要注意的地方
基本介绍和核心接口 1.quartz是完全基于java的可用于进行定时任务调度的开源框架,使用的时候需要引入: <dependency> <groupId>org.quartz ...
- HDU 3507 Print Article(斜率优化)
显然的斜率优化模型 但是单调队列维护斜率单调性的时候出现了莫名的锅orz 代码 #include <cstdio> #include <algorithm> #include ...
- (转载)一张表搞清楚西门子S7系列标准DB块与优化DB块
在TIA Portal中为S7-1200/S7-1500 CPU 添加一个 DB 块时,其缺省属性为优化的 DB ,优化的 DB 块与标准的 DB 块整体对比如下表所示: 项 标准 DB 优化 DB ...
- ifconfig 输出里没有IP地址
转载: http://blog.csdn.net/johnstrive/article/details/5625121 inet addr:....Bcast:.....Mask:255.255.25 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- python爬虫训练——爬poj题目
首先要解决的就是不同的题目在不同的页上,也就是要实现翻页功能,自动获取所要爬取的地址,通过分析可以得出不同的页面也就是volume=后面的数字不同 所以我们可以用re模块来替换即可: new_url ...
- 【转】myeclipse 自定义视图Customize Perspective 没有反应
官网查了下,解释如下: 附上链接https://www.myeclipseide.com/PNphpBB2-viewtopic-t-30151.html,大概意思是按如下图所示步骤更新即可.读者可 ...
- super()、this属性与static静态方法的执行逻辑
1.super的构造顺序:永远优先构造父类的方法 2.static永远在类实例之前执行,this的使用范围为实例之后
- 点击返回上一页 wx.navigateTo不管用了
做跳转的时候,发现想返回上一页,但是这个上一页又是tab上的页面,返回不了怎么办呢 wx.navigateTo({ url: '../search/search', }) 解决方法: wx.reL ...
- React Native原生模块向JS传递数据的几种方式(Android)
一般情况可以分为三种方式: 1. 通过回调函数Callbacks的方式 2. 通过Promises的异步的方式 3. 通过发送事件的事件监听的方式. 参考文档:传送门