LeetCode:Reverse Words in a String
2016-05-31 11:38
507 查看
Reverse Words in a String
Total Accepted: 102953 TotalSubmissions: 655369 Difficulty: Medium
Given an input string, reverse the string word by word.
For example,
Given s = "
the sky is blue",
return "
blue is sky the".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
click to show clarification.
Subscribe to see which companies asked this question
Hide Tags
String
Hide Similar Problems
(M) Reverse Words in a String II
思路:
s中两边可能有空格,单词之间可能有多个空格,这些都需要去掉。
用正则表达式就是爽~~~
java code:
public class Solution { public String reverseWords(String s) { String[] strs = s.trim().split("\\s+"); String ans = ""; for(int i=strs.length-1;i>=0;i--) { if(i!=0) ans += strs[i]+" "; else ans += strs[i]; } return ans; } }
相关文章推荐
- pomelo 安装 windows 下
- Zabbix利用msmtp+mutt发送邮件报警
- 最少拦截系统(Greedy)
- 配置好了项目,启动tomcat提示异常信息:IOException while loading persisted sessions: java.io.EOFException
- CentOS安装mysql
- 结构化 数据库 代码
- 友盟 集成新浪微博的坑
- 对博客的自我感觉
- 去掉一个表的多个字段重复的记录
- 5.JVM三大性能调优参数:-Xms -Xmx -Xss
- 看了《spring技术内幕》简单的图解总结下spring一些请求调用流程
- shell脚本的调试(trap、tee、shell选项)
- 加载数据之前的加载等待中效果
- day7—直播内容(元昊老师著)
- Python脚本性能分析
- jQuery页面加载初始化的3种方法
- Object类
- iOS使其支持侧滑返回
- android studio 使用快捷键
- 特性值长文本函数读取的用法(BAPI_CHARACT_GETLONGTEXT)