题解 | #单词倒排#

单词倒排

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

字符遍历判断是否是字母 + 栈

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String input = sc.nextLine();
            Deque<String> wordStack = new LinkedList<>();// stack to hold words
            StringBuilder builder = new StringBuilder();
            for (char c : input.toCharArray()) {
                if (('a' <= c && 'z' >= c) || ('A' <= c && 'Z' >= c))  {
                    builder.append(c);// construct word
                } else {
                    wordStack.push(builder.toString());
                    builder.setLength(0);// clear StringBuilder
                }
            }
            wordStack.push(builder.toString());
            System.out.print(String.join(" ", wordStack));// connect words with "space"
        }
    }
}
全部评论

相关推荐

字节 飞书绩效团队 (n+2) * 15 + 1k * 12 + 1w
点赞 评论 收藏
分享
安静的垂耳兔在泡澡:ks已经第八次投递了,它起码挂了还让你再投,不错了
点赞 评论 收藏
分享
10-25 00:32
香梨想要offer:感觉考研以后好好学 后面能乱杀,目前这简历有点难
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务