LeetCode 151 Reverse Words in a String
2015-12-02 10:23
417 查看
题目描述
Given an input string, reverse the string word by word.For example,
Given s = “the sky is blue”,
return “blue is sky the”.
Clarification:
[code]What constitutes a word? A sequence of non-space characters constitutes a word. Could the input string contain leading or trailing spaces? Yes. However, your reversed string should not contain leading or trailing spaces. How about multiple spaces between two words? Reduce them to a single space in the reversed string.
分析
常规的做法是:将字符串首尾的空格(leading or trailing spaces)去掉
翻转每个单词
翻转整个字符串
例如字符串:“the sky”
翻转前:
[code]the sky
翻转每个单词:
[code]eht yks
翻转整个字符串:
[code]sky the
利用Java的API,解这个题目只需要3行代码。
参考自:Reverse Words in a String
代码
[code] public static String reverseWords(String s) { List<String> words = Arrays.asList(s.trim().split(" +")); Collections.reverse(words); return String.join(" ", words); }
相关文章推荐
- c++多态实现的机制
- Unix Network Programming(6)---套接字地址结构
- Android RadioGroup及RadioButton的使用方法
- 在html页面中展示JSON
- [Linux] Chang DNS Setting on Linux
- LeetCode-- Merge Sorted Array
- 通过点击jqgrid表格弹出需要的表格数据
- 常用Mysql命令
- 原生JavaScript实现异步多文件上传
- Android大神
- C++匿名对象调用成员函数
- 下一代 Hadoop YARN :相比于MRv1,YARN的优势
- Sublime text 2/3 中 Package Control 的安装与使用方法
- 哪几个数的阶乘末尾有n个零?
- Android开源框架Universal-Image-Loader详解
- zoc backspace无法使用解决
- 转载:光荣之路/Jmeter学习--1
- 015ListView与SimperAdapter
- ethtool 在 Linux 中的实现框架和应用
- 如何登陆亚马逊EC2