【LeetCode】Reverse Words in a String
来源:互联网 发布:网络监控代理 编辑:程序博客网 时间:2024/05/21 07:13
Reverse Words in a String
Total Accepted: 357 Total Submissions: 2355 My Submissions
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
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.
【解题思路】
1、扫描字符串,按照字符去处理,遇到空格则之前为一个完整的字符串。全部放入list,最后逆序返回。
2、替换掉字符串前后的空白,将中间的多个空格替换为一个,按照正则空格分隔为字符串,逆序返回。
Total Accepted: 357 Total Submissions: 2355 My Submissions
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
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.
【解题思路】
1、扫描字符串,按照字符去处理,遇到空格则之前为一个完整的字符串。全部放入list,最后逆序返回。
2、替换掉字符串前后的空白,将中间的多个空格替换为一个,按照正则空格分隔为字符串,逆序返回。
Java AC 1
public class Solution { public String reverseWords(String s) { if(s == null || "".equals(s.trim())){ return s.trim(); } List<String> list = new ArrayList<String>(); s = s.trim(); StringBuffer sb = new StringBuffer(); int i = 0; int len = s.length(); while(i < len){ sb = new StringBuffer(); while(i < len && s.charAt(i) != ' '){ sb.append(String.valueOf(s.charAt(i))); i++; } while(i < len && s.charAt(i) == ' '){ i++; } list.add(sb.toString()); } int size = list.size(); sb = new StringBuffer(); for(i = size-1; i >= 0; i--){ sb.append(list.get(i)+" "); } return sb.toString().trim(); }}
update 2014-06-10
Java AC 2public class Solution { public String reverseWords(String s) {s = s.trim().replaceAll("[ ]+", " ");String sArr[] = s.split(java.util.regex.Pattern.quote(" "));StringBuffer sb = new StringBuffer();int len = sArr.length;for (int i = len - 1; i >= 0; i--) {sb.append(sArr[i] + " ");}return sb.toString().trim();}}Python AC 2
class Solution: # @param s, a string # @return a string def reverseWords(self, s): sArr = s.strip().split() slen = len(sArr) news = '' for i in range(slen-1, -1, -1): news += sArr[i] news += ' ' return news.strip()
0 0
- Leetcode - string - Reverse Words in a String
- Reverse words in a String leetcode
- [LeetCode]Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- Reverse Words in a String (LeetCode)
- LeetCode Reverse Words in a String Total
- LeetCode(151)Reverse Words in a String
- Reverse Words in a String -- LeetCode
- leetcode Reverse Words in a String
- LeetCode OJ:Reverse Words in a String
- LeetCode Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- [Leetcode] Reverse Words in a String
- Reverse Words in a String[LeetCode]
- LeetCode OJ :Reverse Words in a String
- Leetcode: Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- Oracle中RAISE异常深入分析
- Java处理无法得到上传原始路径的笨办法
- CCArmature动画播放完后执行回调函数的方法
- Mysql 分步查询
- vb 6.0 将sql查询出来的结果保存成excel 解决大数据存储速度
- 【LeetCode】Reverse Words in a String
- 2014年有哪些事情不值得你为之操心
- Vitamio中文API文档(1)
- linux下intel 82579LM 网卡驱动安装
- 【setSVMDetector+新模型】利用Hog特征和SVM分类器进行行人检测
- Linux/Mac下批量替换文件中的字符串
- 测试代码段的执行时间(精确到NSTimeInterval级别)
- 今天开了一个博客,开始写我的自学笔记了!
- 在android中使用lua脚本