题解 | #单词倒排#

单词倒排

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

import java.util.*;
import java.lang.*;

/**
难点:
    while(i>=0 && Character.isLetter(c)){
                i--;
                c = str.charAt(i);
            }
            res.append(str.substring(i+1,j+1)+" ");
    这样写会出现越界错误,因为当i=0时,i--会变成-1,str.charAt(-1)会报错。所以不如直接去掉c
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        StringBuilder res = new StringBuilder();
        int n = str.length();
        int i = n - 1;
        int j = n - 1;

        while (i >= 0) {
            while (i >= 0 && Character.isLetter(str.charAt(i))) {
                i--;
            }
            res.append(str.substring(i + 1, j + 1) + " ");

            while (i >= 0 && !Character.isLetter(str.charAt(i))) {
                i--;
            }
            j = i;
        }
        System.out.println(res.toString().trim());
    }
}

全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
11-03 13:18
门头沟学院 Java
包行:平时怎么刷算法题的哇,字节的手撕听说都很难
字节跳动工作体验
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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