Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
来源:互联网 发布:淘宝开专卖店 编辑:程序博客网 时间:2024/05/16 19:11
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: “Let’s take LeetCode contest”
Output: “s’teL ekat edoCteeL tsetnoc”
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
/** * 把一个字符串中的每个单词反向输出! * 思路: * 1.先把一个字符串转换成字符串数组、 * 2.每一个单词利用StringBuffer的方法进行Reverse操作 * 3.再合并操作 * 4.裁剪字符串的首位的空格 * @param s * @return */ public static String reverseWords(String s) { String[] revSplit = s.split(" "); String revString = ""; for (int i = 0; i < revSplit.length; i++) { revSplit[i] = new StringBuffer(revSplit[i]).reverse().toString(); revString = revString +" " +revSplit[i]; } return revString.trim(); }
但是感觉这样执行效率不怎么高。
Just Beats 9.02%!
阅读全文
0 0
- Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
- LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)
- leetcode Reverse Words in a String III 反转字符串中的单词
- Reverse Words in a String 字符串中单词反转
- [LeetCode]557. Reverse Words in a String III(反转字符串 III)
- Reverse Words in a String III 翻转字符串中的单词之三
- 151. Reverse Words in a String 翻转字符串中的单词
- 557. Reverse Words in a String III (字符串)
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 编译原理(七) 算符优先分析法(构造算符优先关系表算法及C++实现)
- 伪北漂....
- 学习C#以及C还有数据库
- SSE指令集优化学习:双线性插值
- 腾讯网移动端H5页面设计实战分享
- Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
- 防止SQL注入策略
- 二叉树的存储结构及遍历算法
- 实验4:栈和队列的基本操作实现及其应用3
- Codeforces Round #443 (Div. 2)A. Borya's Diagnosis
- MAC平台下cocos2d-x项目打包相关流程(自己总结的)
- [ACL2017]Adversarial Multi-Criteria Learning for Chinese Word Segmentation
- 很好用的Xpath插件
- 软件功能设计