2.4.6

Posted w-j-c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.4.6相关的知识,希望对你有一定的参考价值。

question:

Using the conventions of EXERCISE 2.4.1, give the sequence of heaps produced when the operations P R I O * R * * I * T * Y * * * Q U E * * * U * E are performed on an initially empty max-oriented heap.

answer:

import edu.princeton.cs.algs4.*;

public class RestSequence
{
  private static class MaxPQ<Key extends Comparable<Key>>
{
    private Key[] pq;
    private int N = 0;
    
    public MaxPQ(int maxN)
    {
        pq = (Key[]) new Comparable[maxN + 1];
    }
    
    public boolean isEmpty()
    {
        return N == 0;
    }
    
    public int size()
    {
        return N;
    }
    
    public void insert(Key v)
    {
        pq[++N] = v;
        swim(N);
    }
    
    public Key delMax()
    {
        Key max = pq[1];
        exch(1,N--);
        pq[N+1] = null;
        sink(1);
        return max;
    }
    
    private boolean less(int i, int j)
    {
        return pq[i].compareTo(pq[j]) < 0;
    }
    
    private void exch(int i, int j)
    {
        Key t = pq[i];
        pq[i] = pq[j];
        pq[j] = t;
    }
    
    private void swim(int k)
    {
        while(k > 1 && less(k/2,k))
        {
            exch(k/2,k);
            k/=2;
        }
    }
    
    private void sink(int k)
    {
        while(2*k <= N)
        {
            int j = 2*k;
            if(j < N && less(j,j+1)) j++;
            if(!less(k,j)) break;
            exch(k,j);
            k = j;
        }
    }
    
     private void show()
    {
        for(int i = 1; i <= N; i++)
            StdOut.print(pq[i] + " ");
        StdOut.println();
    }
}
   public static void main(String[] args)
    {
        int N = 20;
        MaxPQ<String> pq = new MaxPQ<String>(N);
        String[] a = {"P","R","I","O","*","R","*","*","I","*","T","*","Y","*","*","*","Q","U","E","*","*","*","U","*","E"};
        for(int i = 0; i < a.length; i++)
        {
            if(a[i] == "*")
            {
                if(!pq.isEmpty())
                {
                    StdOut.println("delete " + pq.delMax());
                }
            }
            else
            {
                StdOut.println("insert " + a[i]);
                pq.insert(a[i]);   
            }
            pq.show();
        }
    }
}

 

以上是关于2.4.6的主要内容,如果未能解决你的问题,请参考以下文章

JeecgBoot 2.4.6 版本发布,基于代码生成器的企业级低代码平台

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板