简体   繁体   English

Java ArrayDeque - offerLast & pollFirst vs offerFirst & pollLast

[英]Java ArrayDeque - offerLast & pollFirst vs offerFirst & pollLast

I was doing some simple algorithm questions and playing around with ArrayDeque.我正在做一些简单的算法问题并玩弄 ArrayDeque。

For example this one:比如这个:

https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/ https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/

When using BFS, I noticed that offerLast & pollFirst / offerFirst & pollLast both give the same (correct) answer.使用 BFS 时,我注意到 offerLast 和 pollFirst / offerFirst 和 pollLast 都给出了相同(正确)的答案。

Is there a best practice for when to use which?何时使用哪个有最佳实践吗? Since this is supposed to be a queue, I think we should offerLast and use pollFirst.由于这应该是一个队列,我认为我们应该提供最后并使用 pollFirst。 But why does offerFirst / pollLast also work?但是为什么 offerFirst / pollLast 也有效?

 public int maxDepth(TreeNode root) {
        // do BFS
        if (root == null) return 0;
        
        int depth = 0;
        ArrayDeque<TreeNode> queue = new ArrayDeque();
        queue.offerLast(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++){
                TreeNode curr = queue.pollFirst();
                if (curr.left != null) queue.offerLast(curr.left);
                if (curr.right != null) queue.offerLast(curr.right);
            }
            depth++;
        }
        
        return depth;
    }

Also, Im aware that we dont need the queue to be of type ArrayDeque.另外,我知道我们不需要队列为 ArrayDeque 类型。 Just a simple Queue would work and a Queue only provides offer/poll which default to the FIFO implementation (offerLast, pollFirst)只需一个简单的队列即可工作,而队列仅提供默认为 FIFO 实现的报价/轮询(offerLast,pollFirst)

But why does offerFirst / pollLast also work?但是为什么 offerFirst / pollLast 也有效?

Because the "start" and "end" of the queue are really just arbitrary decided.因为队列的“开始”和“结束”实际上只是任意决定的。

You can call the first element the "start" and the last element the "end".您可以将第一个元素称为“开始”,将最后一个元素称为“结束”。 In that case, offerLast enqueues, and pollFirst dequeues.在这种情况下, offerLastpollFirst出队。

Since array deques are linear data structures, you can "flip it around" and say, I'll call the first element the "end" of the queue, and the last element the "start" of the queue.由于数组双端队列是线性数据结构,因此您可以“翻转”并说,我将第一个元素称为队列的“结束”,将最后一个元素称为队列的“开始”。 If you think like this, then you would enqueue by calling offerFirst and dequeue by pollLast .如果您是这样想的,那么您将通过调用offerFirst入队并通过pollLast出队。

Another way to explain this is to show that these are just two perspectives:解释这一点的另一种方法是表明这些只是两个观点:

Let's say we are at standing at two ends of the deque.假设我们站在双端队列的两端。 We both think that the element closest to us is the "first element" of the deque.我们都认为离我们最近的元素是双端队列的“第一个元素”。

   the deque;
  a b c d e f g h
^                 ^
|                 |
you               me

You put down a new element right next to a .你在 a 旁边放了a新元素。 From your perspective, this is offerFirst .从您的角度来看,这是offerFirst From my perspective, you would be doing offerLast !从我的角度来看,你会做offerLast

Now let's say you removed h .现在假设您删除了h From your perspective, this is pollLast .从您的角度来看,这是pollLast From my perspective, that would look like pollFirst - you are removing your "last element", but it's my "first element".从我的角度来看,这看起来像pollFirst - 你正在删除你的“最后一个元素”,但它是我的“第一个元素”。

However, note that officially, it is decided that the first element is the start of the queue, and the last element is the end - this is how ArrayDeque implements the Queue interface - so if you call methods from Queue , like offer , it will call offerLast .但是,请注意,官方决定第一个元素是队列的开头,最后一个元素是结尾——这就是ArrayDeque实现Queue接口的方式——所以如果你从Queue调用方法,比如offer ,它将致电offerLast

Although it still works if you have your own notion of start and end, it is still a good habit to follow the official definitions of "start" and "end" (actually just use offer and poll .).虽然如果你有自己的 start 和 end 概念,它仍然有效,但遵循“start”和“end”的官方定义仍然是一个好习惯(实际上只使用offerpoll 。)。 If you use pollLast and offerFirst , your code might not work if you pass your deque to another piece of code expecting a Queue .如果您使用pollLastofferFirst ,如果您将 deque 传递给另一段需要Queue的代码,您的代码可能无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM