当前位置: 首页 > article >正文

双端队列--java--黑马

双端队列

概述

双端队列、队列、栈对比

定义特点
队列一端删除(头),另一端添加(尾)First In First Out
一端删除和添加First In Last Out
双端队列两端都可以添加和删除

双端队列实现

双端队列接口

public interface Deque<E> {
    
    boolean offerFirst(E e);
    boolean offerLast(E e);
    
    public E pollFirst();
    public E pollLast();
    
    public E peekFirst();
    public E peekLast();
    
    boolean isEmpty();
    boolean isFull();
}

使用双向链表实现双端队列

public class LinkedListDeque<E> implements Deque<E>, Iterable<E> {
    
    
    static class Node{
        Node<E> pre;
        E value;
        Node<E> next;
        
        pubilc Node(Node<E> pre, E value, Node<E> next) {
            this.pre = pre;
            this.value = value;
            this.next = next;
        }
    }
    
    int capacity;
    int size;
    Node<E> sentinel = new Node<>(null, null, null);
    
    public LinkedListDeque (int capacity) {
        this.capacity = capacity;
        sentinel.next = sentinel;
        sentinel.pre = sentinel;
    }
    
    @Override 
    public boolean offerFirst(E value) {
        if (isFull()) {
            return false;
        }
        Node<E> a = sentinel;
        Node<E> b = sentinel.next;
        Node<E> added = new Node<>(a, value, b);
        a.next = added;
        b.pre = added;
        size++;
        return true;
    }
    
    @Override 
    public boolean offerLast(E value) {
        if (isFull()) {
            return false;
        }
        Node<E> a = sentinel.pre;
        Node<E> b = sentinel;
        Node<E> added = new Node<>(a, value, b);
        a.next = added;
        b.pre = added;
        size++;
        return true;
    }
    
    @Override 
    public E pollFirst() {
        if (isEmpty()) {
            return null;
        }
        Node<E> a = sentinel;
        Node<E> removed = a.next;
        Node<E> b = removed.next;
        a.next = b;
        b.pre = a;
        size--;
        return removed.value;
    }
    
    @Override
    public E pollLast() {
        if (isEmpty()) {
            return null;
        }
        Node<E> b = sentinel;
        Node<E> removed = b.pre;
        Node<E> a = b.pre;
        a.next = b;
        b.pre = a;
        size--;
        return removed.value;
    }
    
    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.next.value;
    }   
    
    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.pre.value;
    }
    
    @Override
    public boolean isEmpty() {
        return size == 0;
    }
    
    @Override
    public boolean isFull() {
        return size == capacity;
    }
    
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p = sentinel.next;
            @Override
            public boolean hasNext() {
                return p != sentinel;
            }
            
            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        }
    }
}

使用数组实现

public class ArrayDeque<E> implements Deque<E>, Iterable<E> {
    
    
    
    static int inc(int i, int length) {
        if(i + 1 > length) {
            return 0;
        }
        return i + 1;
    }
    
    static int des(int i, int length) {
        if (i - 1 < 0) {
            return length - 1;
        }
        return i - 1;
    }
    
    E[] array;
    int head;
    int tail;
    
    public ArrayDeque(int capacity) {
        this.array = (E[]) new Object[capacity + 1];
    }
    
    @Override 
    public boolean offerFirst(E value) {
        if (isFull()) {
            return false;
        }
        head = des(head, array.length);
        array[head] = value;
        return true;
    }
    
    @Override 
    public boolean offerLast(E value) {
        if (isFull()) {
            return false;
        }
        array[tail] = value;
        tail = inc(tail, array.length);
        return true;
    }
    
    @Override 
    public E pollFirst() {
        if (isEmpty()) {
            return null;
        }
        E value = array[head];
        array[head] = null;
        head = inc(head, array.length);
        return value;
    }
    
    @Override
    public E pollLast() {
        if (isEmpty()) {
            return null;
        }
        tail = des(tail, array.length);
        E value = array[tail];
        array[tail] = null;
        return value;
    }
    
    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return array[head];
    }   
    
    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        tail = des(tail, array.length);
        return array[tail];
    }
    
    
    @Override 
    public boolean isEmpty() {
        return head == tail;
    }
    
    @Override
    public boolean isFull() {
        if (head < tail) {
            return tail - head = array.length - 1;
        } else if (head > tail) {
            return head - tail == 1;
        } else {
            return false;
        }
    }
    
    @Override
    public Iterator<E> iterator() {
        int p = head;
        return new iterator() {
            @Override
            public boolean hasNext() {
                return p != tail;
            }
            
            @Override
            public E next() {
                E value = array[p];
                p = inc(p, array.length);
                return value;
            }
        }
    }
    
}

Leetcode102

public class Leetcode102 {
    
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
       	LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int c1 = 1;
        while (!queue.isEmpty()) {
            int c2 = 0;
            List<Integer> list = new ArrayList<>();
            
            for(int i = 0; i < c1; i++) {
                TreeNode n = queue.poll();
                list.add(n.val);
                if (n.left != null) {
                    queue.offer(n.left);
                    c2++;
                }
                if (n.right != null) {
                    queue.offer(n.right);
                    c2++;
                }
            }
            res.add(list);
            c1 = c2;
        }
        return res;
    }
}

Leetcode103

pubic class Leetcode103{
    
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
       	LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int c1 = 1;
        boolean odd = true;
        while (!queue.isEmpty()) {
            int c2 = 0;
            LinkedList<Integer> list = new LinkedList<>();
            
            for(int i = 0; i < c1; i++) {
                TreeNode n = queue.poll();
                if (odd) {
                    list.offerLast(n.val);
                } else {
                    list.offerFirst(n.val);
                }
                if (n.left != null) {
                    queue.offer(n.left);
                    c2++;
                }
                if (n.right != null) {
                    queue.offer(n.right);
                    c2++;
                }
            }
            res.add(list);
            c1 = c2;
            odd = !odd;
        }
        return res;
    }
    
}

http://www.kler.cn/a/301619.html

相关文章:

  • Excel SUMIFS
  • mybatis 动态SQL语句
  • STL序列式容器之stack
  • 计算机组成与原理(2) basic of computer architecture
  • Linux:进程的优先级 进程切换
  • 力扣题目解析--括号生成
  • Python 学习笔记(二)
  • Java后端开发(十六)-- JavaBean对象拷贝工具类:运用反射机制,实现对象的深拷贝
  • 指针与二维数组
  • 软考中级软件设计师-【计算机系统】必考题汇总
  • 【Java】解决项目启动时端口被占用
  • 【Linux取经之路】用户权限管理
  • 博客常见问题
  • WordPress上可以内容替换的插件
  • FreeRTOS学习笔记(八)事件
  • 个人学习笔记7-3:动手学深度学习pytorch版-李沐
  • 深圳建站公司-如何做网站
  • Navicat BI 中创建自定义字段:计算字段
  • 【隐私计算】Paillier半同态加密算法
  • 【Colab代码调试】End-to-end reproducible AI pipelines in radiology using the cloud
  • 【时时三省】c语言例题----华为机试题<统计字符>
  • JVM面试(六)垃圾收集器
  • Linux下的Makefile与进度条程序
  • STM32重定义printf,实现串口打印
  • 鸿蒙轻内核A核源码分析系列五 虚实映射(5)虚实映射解除
  • maven简介