小编jam*_*one的帖子

数组到二进制搜索树快速

给定一个整数数组,有没有办法将其快速转换为二进制搜索树(不平衡)?我尝试为每个元素一个一个地插入它,但这意味着我必须从每个插入开始一遍。它可以完美工作,但我认为最坏的情况是O(N ^ 2)不平衡,例如,数组已排序。考虑到较大的N,我认为这将需要一些时间。

回到我的问题,有没有一种方法可以比我说的算法更快呢?

例如,给定数组[4,5,2,3,1],有没有快速的方法来创建它?

    4  
   /  \  
  2    5  
 / \  
1   3
Run Code Online (Sandbox Code Playgroud)

algorithm binary-search-tree

5
推荐指数
3
解决办法
1万
查看次数

如何避免在函数(JAVA)中返回NULL值?

假设我有一个List<SomeObject>函数返回该对象的引用(如果可用).

SomeObject GetSomeObject(List<SomeObject>, int x){
    /* Search for object in list that has a properties
        with value x */

    if (found)
        return /* The object found */
    else
        return NULL;
}

void DoSomething(SomeObject S){
    if(S!=NULL){
        /* Do action 1 */
    }
    else{
        /* Do action 2 */
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经读过某个地方,返回NULL不是干净代码的一部分.所以我想知道这个案例的等效代码是什么.

更新:我已经阅读了这个问题,我认为我的情况有所不同.在这种情况下,if NULL返回然后什么都不做,而我需要做一些事情,如果NULL返回

java oop null

4
推荐指数
2
解决办法
4746
查看次数

无法调用其他类C++的析构函数

所以我有这个代码,当我运行它时,当B类试图调用它的析构函数时,我遇到了运行时错误.A类的析构函数似乎很好.我想知道这段代码有什么问题.谢谢你的帮助.

#include <iostream>

using namespace std;

class A{
    public:
        //Constructor
        A(int N){
            this->N = N;
            array = new int[N];
        }

        //Copy Constructor
        A(const A& A1){
            this->N = A1.N;

            //Create new array with exact size
            this->array = new int[N];

            //Copy element
            for(int i=0; i<N; i++){
                this->array[i]= A1.array[i];
            }
        }

        ~A(){
            delete [] array;
            cout<<"Success"<<endl;
        }

        int N;
        int* array;
};

class B{
    public:
        B(const A& array1){
            array = new A(array1);
        }

        ~B(){
            delete [] array;
        }

        A* array;
};

using namespace …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks

0
推荐指数
1
解决办法
67
查看次数

即使没有任何更改,Makefile始终不是最新的

我有一个目录,其中包含两个文件夹,src并且binmakefile位于根目录中。即使没有更改,此makefile仍会继续编译(不是最新的)。我是否缺少此makefile文件?

all:
    make a b

a: ./src/a.cpp
    g++ -o ./bin/a ./src/a.cpp

b: ./src/b.cpp
    g++ -o ./bin/b ./src/b.cpp
Run Code Online (Sandbox Code Playgroud)

makefile

0
推荐指数
1
解决办法
216
查看次数

标签 统计

algorithm ×1

binary-search-tree ×1

c++ ×1

java ×1

makefile ×1

memory-leaks ×1

null ×1

oop ×1