题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
java 通俗易懂版
转化成char[] 利用两个变量 index end ---> 找到单词
import java.util.Scanner; public class H31 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = " " + sc.nextLine(); int index = 0; int end = 0; StringBuilder sb = new StringBuilder(); char[] chars = str.toCharArray(); for (int i = chars.length - 1; i >= 0; i--) { if (Character.isLowerCase(chars[i]) || Character.isUpperCase(chars[i])) { index = index == 0 ? i : index; } else { end = index != 0 ? i : index; } if (index != 0 && end != 0) { sb.append(str.substring(end+1, index+1) + " "); index = 0; end = 0; } } System.out.println(sb.toString()); } }