[Leetcode] reverse words in a string 反转单词
来源:互联网 发布:js对象概念 编辑:程序博客网 时间:2024/05/17 04:30
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
思路:
(1)首先去掉首尾的White Space等字符,利用Java String 的trim函数
(2)通过空格分割字符串,split(“\\s+”),通过正则匹配两单词间的多个空格。
(3)StringBuffer 添加倒序遍历的Split完后的数组。
public String reverseWords(String s) { StringBuffer sb = new StringBuffer(); s =s.trim(); String []ary = s.split("\\s+"); if(s.length()==0) return ""; for(int i = ary.length;i>0;i--){ if(!ary[i-1].equals(" ")&&i==ary.length)sb.append(ary[i-1]); else if(!ary[i-1].equals(" ")&&i!=ary.length) sb.append(" "+ary[i-1]); } return sb.toString(); }
0 0
- [LeetCode]Reverse Words in a String-反转单词
- [Leetcode] reverse words in a string 反转单词
- LeetCode 151. Reverse Words in a String(反转单词)
- Reverse Words in a String 字符串中单词反转
- Reverse Words in a String 反转单词顺序
- LeetCode 186. Reverse Words in a String II(反转单词)
- LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)
- leetcode Reverse Words in a String III 反转字符串中的单词
- LeetCode Reverse Words in a String(反转字符串)
- LeetCode Reverse Words in a String(反转字符串)
- 字符串反转 reverse-words-in-a-string @LeetCode
- [leetcode, python] Reverse Words in a String 反转字符串
- 【LeetCode】{Reverse Words in a String} 单词间只留一个空格
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
- Leetcode - string - Reverse Words in a String
- Reverse words in a String leetcode
- [LeetCode]Reverse Words in a String
- 昆虫繁殖
- C语言实现 小球自由落体问题
- 2014-7-16任务完成情况
- poj 1038 Bugs Integrated, Inc.(动态规划状态压缩)
- ctags的安装和使用
- [Leetcode] reverse words in a string 反转单词
- Animation动画效果的实现(四)------总结
- C#中通过com组件操作excel不能关闭的问题
- SSD的SMART属性
- 要做的事
- UINavigationController使用详解
- C语言比较 回车符和零的区别
- 软件工程
- POJ1680