[LeetCode]Reverse Words in a String
来源:互联网 发布:unity3d 鼠标中键平移 编辑:程序博客网 时间:2024/05/16 11:30
题目:给定一个字符串数组,翻转数组中的单词,样例:the sky is blue 输出 blue is sky the
算法:字符串处理,要注意一些细节:
1. 字符串为空
2. 字符串由空白组成
3. 字符串前度和后度空白
4. 字符串之间充满了空白
public class Solution { public String reverseWords(String s) { if (null == s) { // case when s is null return null; } int length = s.length(); ArrayList<String> words = new ArrayList<String>(); for (int i=0; i<length; ) { String word = new String(); while (i<length && (' '==s.charAt(i) || '\t'==s.charAt(i))) { ++i; } while (i<length && ' '!=s.charAt(i) && '\t'!=s.charAt(i)) { word += s.charAt(i++); } if (word.length() >= 1) { words.add(word); } } if (words.isEmpty()) { return ""; } else { String reverse = new String(); boolean isFirstWord = true; int nWords = words.size(); for (int j=nWords-1; j>=0; --j) { if (isFirstWord) { reverse += words.get(j); isFirstWord = false; } else { reverse += (" " + words.get(j)); } } return reverse; } }}
3 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
- 百度首席科学家 Andrew Ng谈深度学习的挑战和未来
- winform 菜单项显示历史记录
- shell-here
- 巧用QSignalMapper分类有序地处理大量信号(含源码)
- POJ 1979 Red and Black【深度优先搜索】
- [LeetCode]Reverse Words in a String
- my eclipse8.6发送邮件错误解决
- 一张图片表述数字证书的签名与验证原理
- Unity上使用Linq To XML——简单易维护的代码
- Qt 4.5.3 +tslib1.4移植
- XShell和Xftp
- first vba
- NSNotificationCenter 通知中心
- Poj1363