题解 | #单词倒排#

单词倒排

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

双指针

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);

        String str = br.readLine();
        int left = str.length() - 1, right = str.length() - 1;
        while (left >= 0) {
            while (left >= 0 && Character.isLetter(str.charAt(left))) {
                left--;
            }
            pw.printf("%s ", str.substring(left + 1, right + 1));
            while (left >= 0 && !Character.isLetter(str.charAt(left))) {
                left--;
            }
            right = left;
        }
        pw.println();

        pw.flush();
        pw.close();
        br.close();
    }
}
全部评论

相关推荐

迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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