题解 | #单词倒排#
单词倒排
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); scanner.useDelimiter("\n"); String s1 = null; if (scanner.hasNext()) { s1 = scanner.next(); } if (s1 == null || s1.length()==0) return; StringBuilder builder = new StringBuilder(); StringBuilder builder2 = new StringBuilder(); for (int i = s1.length()-1;; i--) { if (i<0) { builder.append(builder2.reverse()); break; } if ((s1.charAt(i) >= 65 && s1.charAt(i) <= 95) || (s1.charAt(i) >= 97 && s1.charAt(i) <= 122)) { builder2.append(s1.charAt(i)); } else if (builder2.length()!=0){ builder.append(builder2.reverse()); builder.append(" "); builder2.delete(0, builder2.length()); } } System.out.println(builder); } }