题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<String> list = new ArrayList<String>(); //从尾开始逐个字符遍历字符串,非26个大小写字母的就丢弃,如果连续的字母就放在一个list中 String model = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; String str = in.nextLine(); int i = str.length() - 1; int end = 0; while(i >= 0) { if(model.contains(str.substring(i,i+1))) { end++; if(i == 0) { list.add(str.substring(i,i + end)); break; } }else{ if(end != 0) { list.add(str.substring(i+1,i + 1 + end)); } end = 0; } i--; } for(int m = 0; m < list.size(); m++) { System.out.print(list.get(m) + " "); } } }