题解 | #单词倒排#

单词倒排

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

有序输出问题首先可以想到使用有序队列Deque实现,将单个String对象从头插入队列后使用String.join方法以空格为分割的方式遍历输出,输入字符串使用正则表达式进行切割

public class Algorithm {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] strs = str.split("[^A-Za-z]");
        Deque<String> dq = new LinkedList<>();
        for (String s : strs){
            dq.addFirst(s);
        }
        System.out.println(String.join(" ", dq));
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务