LeetCode 1 Reverse Words in a String
来源:互联网 发布:阿里旺旺网络连接失败 编辑:程序博客网 时间:2024/05/21 00:45
Given an input string, reverse the string word by word.
For example:
Given s = "the sky is blue",
return "blue is sky the".
原题链接
这道题没有什么技巧,主要是考察对字符串操作的掌握。
注意点:
1,对一个空字符串使用split(“ ”)会返回一个长度为1的空格字符串;
2,split(" ")对每一个空格都起作用,所以如果想把相连的空格当做一个处理,应该用split(" +"),即一个或多个空格;
3,用StringBuilder代替String来避免不必要空间的开辟。
public class Solution { public String reverseWords(String s) { if(s==null) return null; String newStr = s.trim(); if(newStr.length()==0) return newStr; String[] words = newStr.split(" +"); StringBuilder sb = new StringBuilder(); for(int i=words.length-1; i>=0; i--){ sb.append(words[i]); if(i!=0){ sb.append(" "); } } return sb.toString(); }}
0 0
- LeetCode 1 Reverse Words in a String
- [leetcode] Reverse Words in a String [1]
- (leetcode 1)Reverse Words in a String
- LeetCode 1 Reverse Words in a String
- Reverse Words in a String【leetcode 1】
- 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
- Linux 网络开发常见面试题回顾
- POJ-1539(表达式求值)
- Uva1626 线性DP
- 自用收藏的链接
- 给定一整型数组,若数组中某个下标值大的元素值小于某个下标值比它小的元素值,称这是一个反序
- LeetCode 1 Reverse Words in a String
- 2大类型的设备驱动程序(2 main types of device driver)
- POJ-1400(删除冗余括号)
- 迷茫
- UVA307
- 实现算法2.4的程序
- Airport(凸包加点与直线的距离)
- 在VS2010下建立cocos2dx项目
- Leetcode--Reorder List