您的位置:首页 > 其它

LeetCode----------------------Reverse Words in a String

2015-09-04 13:45 501 查看
Given an input string, reverse the string word by word.

For example,

Given s = "
the sky is blue
",

return "
blue is sky the
"

程序如下:

public class Solution {

public String reverseWords(String s) {

String[] strings = s .trim().split("\\s+");

ArrayList<String> reverseString = new ArrayList<String>();

int sLength = strings.length;

for(int i = sLength - 1;i >= 0;i--){

reverseString.add(strings[i]);

}

String reverse = reverseString.toString();

reverse = reverse.substring(1,reverse.length()-1).replace(", "," ");

return reverse;

}

}

过程中有点挫折:split()函数匹配的正则表达式没弄清楚,若字符串两段有空格以及两字符串之间有多个字符串应该s .trim().split("\\s+");

下一步学习一下正则表达式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: