【发布时间】:2017-10-12 08:47:51
【问题描述】:
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Let's take LeetCode contest";
reverseWords(str);
}
private static void reverseWords(String str) {
// TODO Auto-generated method stub
String[] words = str.split("\\s");
String reverse = "";
String word="";
ArrayList<String> al = new ArrayList<String>();
for(int i=0 ; i < words.length;i++){
al.add(words[i]);
for(int j=al.get(i).length()-1;j>=0;j--){
reverse=reverse+al.get(i).charAt(j);
word= reverse.replaceAll("..", "$0 ");
}
}
System.out.println(str);
System.out.println(word);
}
这段代码给出的是:"s'teLekatedoCteeLtsetnoc"
但是应该输出预期的输出:"s'teL ekat edoCteeL tsetnoc"
【问题讨论】:
-
您的代码确实不输出您声称的内容。它输出
"s' te Le ka te do Ct ee Lt se tn oc"
标签: java string algorithm data-structures string-matching