翻转单词顺序列
来源:互联网 发布:多益网络客服电话 编辑:程序博客网 时间:2024/06/10 14:47
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
import java.util.*;public class Solution { public static String ReverseSentence(String str) { String result=""; if (str.length() == 0) return ""; ArrayList<String> list = new ArrayList<>(); int i = 0; for (int j = 0; j < str.length(); j++) { if (str.charAt(j) == ' ') { list.add(str.substring(i,j)); i = j+1; } } list.add(str.substring(i, str.length())); Collections.reverse(list); for (int k = 0; k < list.size()-1; k++) { result += (list.get(k)); result += " "; } result += (list.get(list.size()-1)); return result; }}
阅读全文
0 0
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 扑克牌顺子
- sharding-jdbc分库分表规则(2)-多表查询
- day1对象
- Surround the Trees HDU
- Gitolite + repo 搭建安卓源码开发环境
- 翻转单词顺序列
- 使用python在Window10下配置tensorflow
- Codeforces Round #436 (Div. 2) E
- 左旋转字符串
- SpringMVC简例HelloWorld~
- Linux C编程 GCC的使用
- Visual Studio Enterprise 2017 ReferenceManagerPackage的解决方案
- springMvc执行流程简介
- 和为S的两个数字