小编tem*_*def的帖子

检查图是否至少是一种单向连接

有向图被称为“ 在-至少-单向连接的 ”如果,对于每两个节点uv在图中,有两种从路径uv或从一个路径vu(或两者)。
是否有O(m + n)解决此问题的时间复杂度算法?

algorithm big-o graph time-complexity graph-algorithm

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

为什么最小可能整数的否定会产生自身?

所以我使用 C 语言和 64 位机器编写了一个测试下溢和溢出的小实验。对于 int 类型,最小/最大值为:

   int tmax = 2147483647;
   int tmin = -2147483648;
Run Code Online (Sandbox Code Playgroud)

我知道二进制补码是如何工作的,这不是我的问题。

所以我想,如果我做了一些负面的事情会发生什么?那是:

   int tmin = -2147483648;
   int negativeTmin = -tmin;
Run Code Online (Sandbox Code Playgroud)

结果还是tmin。(也就是说,负Tmin将为-2147483648)

我的问题是为什么会这样?由于正 2,147,483,648 不能用 int 表示,我理解为什么它当然不是这样,但它根本没有改变似乎很奇怪,因为这使得它成为应用 - 时唯一不会改变的非零 int到它。我并不是说我对它应该是什么有更好的了解,我只是好奇为什么 -tmin == tmin。它是否与按位运算有关,或者与计算机中如何进行减法有关,或者它是否默认这样做,因为我想要做的事情是未定义的,或者其他什么?

我的代码:

#include <stdio.h>
int main() {
   int tmax = 2147483647;
   printf("tmax Before: %d\n", tmax);
   tmax++;
   printf("tmax After: %d\n\n", tmax);

   int tmin = -2147483648;
   printf("tmin Before: %d\n", tmin);
   tmin--;
   printf("tmin After: %d\n\n", tmin);

   int tmin2 = -2147483648;
   int negativeTmin = -tmin2;
   printf("negative tmin: …
Run Code Online (Sandbox Code Playgroud)

c storage integer min twos-complement

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

C Strcat valgrind错误

我正在尝试连接两个字符串,以便我可以获取文件路径.但是,我在valgrind中收到错误

条件跳转或移动取决于未初始化的值

我的代码:

/**
 * @brief Concatenate two strings to get file path
 * @param firstP - First string
 * @param secondP - Second string
 * @return Returns the concatenated string
 */
char *getPathDir(char *firstP, char *secondP) {
    char *new_str;
    int stringSize = strlen(firstP)+strlen(secondP)+2;

    if((new_str = malloc(stringSize)) != NULL){
        new_str[0] = '\0';
        strcat(new_str,firstP);
        new_str[strlen(firstP)] = '/';
        strcat(new_str,secondP);
    } else {
        perror("malloc");
        cleanUp();
        exit(EXIT_FAILURE);
    }
    return new_str;
}
Run Code Online (Sandbox Code Playgroud)

c string strcat

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

具有解除引用指针的多态性会产生意想不到的结果......为什么?

我遇到了一个C++难题,可以使用一些帮助!请考虑以下代码:

#include <iostream>

struct Node
{
    virtual void print() 
    {
        std::cout << "Node::print" << std::endl;
    }
};

struct Drawable : public Node
{
    virtual void print() 
    {
        std::cout << "Drawable::print" << std::endl;
    }
};

struct Visitor
{
    virtual void apply(Node& node)
    {
        std::cout << "apply(Node&)" << std::endl;
        node.print();
    }
    virtual void apply(Drawable& drawable) 
    {
        std::cout << "apply(Drawable&)" << std::endl;
        drawable.print();
    }
};

struct Renderer
{
    virtual void accept(Node* node, Visitor* visitor)
    {
        visitor->apply(*node);
    }
};

int main(int argc, char** argv) …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism visitor

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

为什么this.super()在Java中不可行?

在下面的示例中,如果我创建一个名为example的类的构造函数,如下所示:

public class Example{

    public Example(){
        this.super();
    }

}
Run Code Online (Sandbox Code Playgroud)

上面的方法不起作用,因为会javac Example.java通知以下编译错误:

Example.java:3: error: illegal qualifier; Object is not an inner class
        this.super();
            ^
1 error
Run Code Online (Sandbox Code Playgroud)

但是,它不应该像this使用隐式声明那样工作,而不是通过使用super()显式声明this吗?

java constructor this super

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

这个 BFS 算法的时间复杂度是多少?

对于问题https://leetcode.com/problems/perfect-squares/我已经使用以下算法解决了它。问题是

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

Example 1:

Input: n = 12
Output: 3 
Explanation: 12 = 4 + 4 + 4.
Run Code Online (Sandbox Code Playgroud)

它所做的基本上是尝试通过减去每个可能的数字([1, 4, 9, .. sqrt(n)] 来从目标数字变为 0,然后对获得的每个数字进行相同的工作。我很难理解这个算法的时间复杂度,因为每个级别的分支都是 sqrt(n) 次,但有些分支注定要提前结束......

def numSquares(n):


        squares = [i**2 for i in range(1, int(n**0.5)+1)]

        step = 1
        queue = {n}

        while queue:
            tempQueue = set()

            for node in queue:
                for square in …
Run Code Online (Sandbox Code Playgroud)

algorithm math breadth-first-search time-complexity

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

为什么在链表中创建当前变量时不使用“new”?

这是打印链表元素的解决方案。

为什么不是Node *current = new Node;然后current = head;呢?

void printLinkedList(Node* head)
{
    Node *current = head;    
    while(current!=NULL){
        cout << current -> data << endl;
        current = current -> next;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list new-operator data-structures

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

什么时候使用选择排序和合并排序?

合并排序 (nlogn) 的效率总是比选择排序 (n^2) 快。你什么时候会选择选择而不是合并排序?

sorting algorithm big-o mergesort selection-sort

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

是否可以在 C++ 中添加到空双端队列的迭代器中?

以下是导致问题的原因的示例:

#include <deque>

int main() {
    std::deque<int> has_data = {1, 2, 3};
    std::deque<int>::iterator iter1 = has_data.begin() + 5; // This works fine
    
    std::deque<int> had_data = {4, 5, 6};
    had_data.clear();
    std::deque<int>::iterator iter2 = had_data.begin() + 5; // This also works fine
    
    std::deque<int> is_empty;
    std::deque<int>::iterator iter3 = is_empty.begin() + 5; // This causes a segfault
}
Run Code Online (Sandbox Code Playgroud)

如果双端队列之前从未包含任何元素,则添加到空双端队列的迭代器似乎只是一个问题。

我很好奇这是否是 STL 中的错误,或者我是否只是以导致未定义行为的方式使用它。我只在使用 Xcode(GUI 和命令行)编译时遇到这个问题。我也在 Linux 上使用 GCC 6.2.0 版尝试过它,但那里似乎不存在问题。

c++ iterator stl stddeque c++17

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

当向下舍入的值不是 0 时,为什么这个浮点除法会产生零?

我正在练习 C++,但发现了一些问题:

#include <iostream>
#include <string>
using namespace std;

int main() {
    int a = 2;
    {
        cout << a;
        cout << "\n";
        float a = a / 2;
        cout << "a= a/2 = ";
        cout << a;

    }
    cout << "\n";
    a = 2;
    {
        cout << a;
        cout << "\n";
        float b = a / 2;
        cout << "b= a/2 = ";
        cout << b;

    }
}
Run Code Online (Sandbox Code Playgroud)

这个回报:

2 
a= a/2 = 0 
2 
b= a/2 = 1 …
Run Code Online (Sandbox Code Playgroud)

c++

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