第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

有沒有辦法在我的代碼中修復(fù)我的 Max heapify

有沒有辦法在我的代碼中修復(fù)我的 Max heapify

qq_笑_17 2022-06-04 15:06:56
問題是我正在嘗試修復(fù)最大 heapify,但由于錯(cuò)誤不斷發(fā)生,它無法正常工作。我一直在關(guān)注幾本書的偽代碼,但仍然顯示錯(cuò)誤。我正在嘗試通過使用to exchangeA[i]來交換to 但它卻給出了錯(cuò)誤A[largest]=class Heap {// public for JUnit testing purposespublic ArrayList<Integer> array;public int heap_size;public Heap(int size) {}public Heap(List<Integer> source) {    this(source, false);}public Heap(List<Integer> source, boolean incremental) {}public static int parent(int index) {    return index/2;}public static int left(int index) {    return 2 * index;}public static int right(int index) {    return (2 * index) + 1;}public void maxHeapify(int i, int A){  int l = left(i);  int r = right(i);  if(l <= A.heap_size && A[l] > A[i])    largest = l;  else    largest = i;  if(r <= A.heap_size && A[r] > A[largest])    largest = r;  if(largest != i)  {    A[i] = A[largest];    maxHeapify(A,largest);  }}public void buildMaxHeap() {}public void insert(Integer k) {}public Integer maximum() {    return 0;}public Integer extractMax() {    return 0; }}我期待它運(yùn)行,但我得到一個(gè)錯(cuò)誤    Heap.java:31: error: int cannot be dereferenced  if(l <= A.heap_size && A[l] > A[i])           ^   Heap.java:31: error: array required, but int found  if(l <= A.heap_size && A[l] > A[i])                          ^  Heap.java:31: error: array required, but int found  if(l <= A.heap_size && A[l] > A[i])                                 ^    Heap.java:32: error: cannot find symbol    largest = l;    ^   symbol:   variable largest   location: class Heap如果可以,請(qǐng)幫忙。
查看完整描述

2 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

==是比較運(yùn)算符。如果要分配值,則應(yīng)使用=運(yùn)算符:

A[i] = A[largest];


查看完整回答
反對(duì) 回復(fù) 2022-06-04
?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

根據(jù)錯(cuò)誤信息,問題在于 maxHeapify 的參數(shù) A 是一個(gè)整數(shù)值。您需要將參數(shù) A 作為數(shù)組傳遞。


public void maxHeapify(int i, int[] A) {

    int largest = i;

    int l = left(i);

    int r = right(i);


    if(l < A.length && A[l] > A[largest])

        largest = l;

    if(r < A.length && A[r] > A[largest])

        largest = r;


    if(largest != i)

    {

        int tmp = A[i];

        A[i] = A[largest];

        A[largest] = tmp;

        maxHeapify(largest, A);

    }

}

我在下面的代碼中測試了上面的 maxHeapify。


public class HeapMain {

    public static void main(String[] args) {

        // Note: ignore the value at index 0

        int[] A = new int[]{-1, 3, 9, 10, 4, 2, 33, 5};

        Heap heap = new Heap(A.length);

        for (int i = heap.parent(A.length - 1); i > 0; i--) {

            heap.maxHeapify(i, A);

        }


        // You will get => 33 9 10 4 2 3 5

        for (int i = 1; i < A.length; i++) {

            System.out.print(A[i]);

            System.out.print(" ");

        }

        System.out.println();

    }

}

注意:parent、left 和 right 方法假設(shè)數(shù)組中堆的節(jié)點(diǎn)是從索引 1 開始保存的。


以下 URL 可能會(huì)有所幫助。


https://www.youtube.com/watch?v=ixdWTKWSz7s


查看完整回答
反對(duì) 回復(fù) 2022-06-04
  • 2 回答
  • 0 關(guān)注
  • 107 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)