题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
ArrayList<String> list = new ArrayList<>();
char[] arr = line.toCharArray();
// 截取连续字母的字符串
int num = 0;
for (int i = 0; i < arr.length; i++) {
if (!Character.isLetter(arr[i])) {
if (num > 0) {
list.add(line.substring(i - num, i));
}
num = 0;
continue;
}
num++;
}
// 再获取最后一个字符串
if (num > 0) {
list.add(line.substring(line.length() - num));
}
Collections.reverse(list);
System.out.println(String.join(" ", list));
}
}