题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); LinkedList<String> list=new LinkedList<>(); String str1=""; for(char ch: str.toCharArray()){ if(ch<='z'&&ch>='a'||ch>='A'&&ch<='Z'){ str1+=ch; } else{ if(str1!=""){ list.add(str1); } str1=""; } } if(str1!=""){ list.add(str1); } String strans=""; for(int i=list.size()-1;i>0;i--){ strans+=list.pollLast()+" "; } strans+=list.pollLast(); System.out.println(strans); } }使用队列,简单易懂