题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
StringBuilder res = new StringBuilder();
char[] array = string.toCharArray();
int right = array.length - 1;
int left = right;
while (left >= 0){
while (Character.isLetter(array[left])){
left--;
if (left == -1){
break;
}
}
if (left == -1){
res.append(string.substring(0,right + 1));
break;
}
res.append(string.substring(left + 1,right + 1) + " ");
if (left == 0){
break;
}
while (!Character.isLetter(array[left])){
left--;
}
right = left;
}
System.out.println(res);
}
}
小天才公司福利 1237人发布