题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { //使用正则表达式将所有非大小写字母全部替换成空格,再依空格将字符分割成字符数组 String[] s = sc.nextLine().replaceAll("[^a-zA-Z]"," ").split(" "); System.out.println(invertedSort(s)); } } private static String invertedSort(String[] s) { StringBuilder builder = new StringBuilder(); for (int i = s.length - 1; i >= 0 ; i--) { builder.append(s[i]); builder.append(" "); } return builder.toString(); } }