题解 | #单词倒排#

单词倒排

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);
             
             
         
     }
}
使用队列,简单易懂

全部评论

相关推荐

点赞 评论 收藏
转发
投递腾讯等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务