题解 | 单词倒排

单词倒排

https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str=sc.nextLine();
        char []words=new char[str.length()+1];
        words[0]=' ';
        for(int i=1;i<=str.length();i++)
            words[i]=str.charAt(i-1);
        StringBuilder ans=new StringBuilder();
        //记录上一个非字符的位置
        int j=str.length();
        //倒序遍历字符串
        for(int i=str.length();i>=0;i--){
            //遇到非字符就将子串添加到ans中
            if(!((words[i]>=65 && words[i]<=90)||(words[i]>=97&&words[i]<=122))){
                ans.append(str.substring(i,j));
                //更新位置
                j=i-1;
                ans.append(" ");
            }
        }
        System.out.print(ans);
    }
}

全部评论
点赞 回复 分享
发布于 10-30 20:58 陕西

相关推荐

11-03 14:57
西北大学 营销
Belltrix:其实就是每根转动一定的角度
点赞 评论 收藏
分享
牛客77743221...:做一段时间,公司出钱送你去缅甸和泰国旅游
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务