Leetcode编译错误:控制流达到非void函数的末尾

huangapple go评论97阅读模式
英文:

Leetcode Compiler error of the control reaching end of non-void function

问题

我是一名新手程序员,试图解决LeetCode上的问题。我理解逻辑,我知道LeetCode后端的主要函数以及按照特定指示返回的方式。但每次我写代码时,它都会抛出一个编译器错误,说控制未达到非void函数的末尾,我似乎无法弄清楚如何解决这个错误。请帮助我。

我编写的代码并一直在尝试在LeetCode上运行的代码(这是问题74:搜索2-D矩阵):

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target){
    int x;
    for(int i=0;i<matrixSize;i++){
        if(target<=matrix[i][*matrixColSize-1])
           x=i;
    }
    if(target>matrix[matrixSize-1][*matrixColSize-1])
           return false;
    for(int j=0;j<*matrixColSize;j++){
        if(matrix[x][j]==target)
            return true;
        if(j==*matrixColSize-1 && matrix[x][j]!=target)
            return false;
    }
}

我似乎找不到代码有什么问题。即使我尝试将return命令移出if循环,它仍然对每个情况返回false。

英文:

I am a newbie programmer, trying to go through questions on leetcode. I understand the logic, I know how the main function is in the back end of leetcode & about returning in a particular instructed way. But every time I write a code it throws a compiler error about the control reaching end of non-void function and I can't seem to figure out how to counter this error. Please help me out.

The code I have written and have been trying to run on leetcode (It is the problem 74: Search a 2-D matrix):

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target){
    int x;
    for(int i=0;i&lt;matrixSize;i++){
        if(target&lt;=matrix[i][*matrixColSize-1])
           x=i;
    }
    if(target&gt;matrix[matrixSize-1][*matrixColSize-1])
           return false;
    for(int j=0;j&lt;*matrixColSize;j++){
        if(matrix[x][j]==target)
            return true;
        if(j==*matrixColSize-1 &amp;&amp; matrix[x][j]!=target)
            return false;
    }
}

I can't seem to find what is wrong with the code. Even if I try to move the return command out of if loop, it just keeps returning false for every

答案1

得分: 2

如果 *matrixColSize 为0(或更糟糕的是负数),for 循环将永远不会执行,您将跳到函数末尾,而不执行任何一个 return 语句。

即使循环执行了,每次都检查是否是最后一次迭代是编写函数的一种令人困惑的方式。只需让它完成循环,如果没有找到匹配项,那么默认情况下可以返回 false

for (int j = 0; j < *matrixColSize; j++) {
    if (matrix[x][j] == target)
        return true;
}
return false;
英文:

If *matrixColSize is 0 (or worse, negative) the for loop will never execute, and you'll fall through to the end of the function without executing one of the return statements.

Even if the loop does execute, checking every time to see if it's the last iteration is a confusing way to write the function. Just let it finish the loop, and if it hasn't found a match, there's no match, so you can return false by default.

    for(int j=0;j&lt;*matrixColSize;j++){
        if(matrix[x][j]==target)
            return true;
    }
    return false;

答案2

得分: 1

在执行这个for循环后:

for(int j=0;j<*matrixColSize;j++){
    if(matrix[x][j]==target)
        return true;
    if(j==*matrixColSize-1 && matrix[x][j]!=target)
        return false;
}

这个函数没有返回任何结果。你需要在for循环后放置一个返回语句。

或者你可以重写循环,以便在循环后返回结果。

例如,可以使用while循环:

int j = 0;

while ( j < *matrixColSize && matrix[x][j] != target ) ++j;

return j != *matrixColSize; 

另外,不清楚为什么第三个参数是指针类型:

int* matrixColSize

注意,在C语言中,你可以使用可变长度数组来代替动态分配的数组。

另一个注意事项是,代替这个for循环:

for(int i=0;i<matrixSize;i++){
    if(target<=matrix[i][*matrixColSize-1])
       x=i;
}

该循环顺序搜索行,你可以实现二分搜索方法作为上限方法,因为根据任务的描述,每行的第一个元素大于前一行的最后一个元素。然后对于选择的行,你可以使用标准的C函数 bsearch

下面是一个伪代码形式的C++程序,演示了如何基于二分搜索方法应用于矩阵的行,然后应用于所选行来实现函数的思路:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

bool searchMatrix( const std::vector<std::vector<int>> &matrix, int target )
{
    std::vector<int> value = { target };

    auto row = std::upper_bound( std::begin( matrix ), std::end( matrix ), value,
        []( const auto &row1, const auto &row2 ) { return row1[0] < row2[0]; } );

    bool success = row != std::begin( matrix );

    if (success)
    {
        std::advance( row, -1 );

        size_t size = row->size();

        success = !( ( *row )[size - 1] < target ) &&
            std::binary_search( std::begin( *row ), std::end( *row ), target );
    }

    return success;
}

int main()
{
    std::vector<std::vector<int>> matrix =
    {
        {  1,  3,  5,  7 },
        { 10, 11, 16, 20 },
        { 23, 30, 34, 60 }
    };

    std::cout << std::boolalpha;
    std::cout << "searchMatrix( matrix, 0 )  is " << searchMatrix( matrix, 0 )  << '\n';
    std::cout << "searchMatrix( matrix, 8 )  is " << searchMatrix( matrix, 8 )  << '\n';
    std::cout << "searchMatrix( matrix, 3 )  is " << searchMatrix( matrix, 3 )  << '\n';
    std::cout << "searchMatrix( matrix, 13 ) is " << searchMatrix( matrix, 13 ) << '\n';
    std::cout << "searchMatrix( matrix, 60 ) is " << searchMatrix( matrix, 60 ) << '\n';
    std::cout << "searchMatrix( matrix, 61 ) is " << searchMatrix( matrix, 61 ) << '\n';
}

程序输出如下:

searchMatrix( matrix, 0 )  is false
searchMatrix( matrix, 8 )  is false
searchMatrix( matrix, 3 )  is true
searchMatrix( matrix, 13 ) is false
searchMatrix( matrix, 60 ) is true
searchMatrix( matrix, 61 ) is false

在你的函数实现中,代替第一个for循环:

for(int i=0;i<matrixSize;i++){
    if(target<=matrix[i][*matrixColSize-1])
       x=i;
}

你应该使用二分搜索方法(由你自己编写)来找到目标行。在上面的C++程序中,使用了标准算法 std::upper_bound

然后,代替第二个for循环,你应该使用标准的C函数 bsearch,因为正如任务描述中所写的那样:

你必须以O(log(m * n))的时间复杂度编写解决方案。

这是任务的主要要求。也就是说,你不能在函数内使用顺序搜索。

更新。我会这样在C中编写该函数:

bool searchMatrix( int **matrix, int matrixSize, int *matrixColSize, int target ) 
{
    bool success = matrixSize > 0 && *matrixColSize > 0;

    if (success)
    {
        int target_row = 0, last_row = matrixSize;

        while (target_row < last_row)
        {
            int middle_row = target_row + ( last_row - target_row ) / 2;

            if (target < matrix[middle_row][0])
            {
                last_row = middle_row;
            }
            else
            {
                target_row = middle_row + 1;
            }
        }

        success = target_row != 0;

        if ( success )
        {
            --target_row;

            success = !( matrix[target_row][*matrixColSize - 1] < target ) &&
                bsearch( &target, matrix[target_row], *matrixColSize, sizeof( int ), cmp ) != NULL;
        }
    }

    return success;
}

我使用了在网站上提供的声明,尽管如我已经指出的,通过指针传递列数没有意义。

cmp 函数可以如下所示:

int cmp( const void *p1, const void *p2 )
{
    int x = *( const int * )p1;
    int y = *( const int * )p2;

    return ( y < x ) - ( x < y );
}
英文:

After executing this for loop

for(int j=0;j&lt;*matrixColSize;j++){
if(matrix[x][j]==target)
return true;
if(j==*matrixColSize-1 &amp;&amp; matrix[x][j]!=target)
return false;
}

the function returns nothing. You need to place a return statement after the for loop.

Or you need to rewrite the loop such a way that the result would be returned after the loop.

For example there could be used while loop as

int j = 0;
while ( j &lt; *matrixColSize &amp;&amp; matrix[x][j] != target ) ++j;
return j != *matrixColSize; 

Also it is unclear why the third parameter has pointer type

int* matrixColSize

Pay attention to that in C you can use variable length arrays that could be used instead of dynamically allocated arrays.

Another remark is that instead of this for loop

for(int i=0;i&lt;matrixSize;i++){
if(target&lt;=matrix[i][*matrixColSize-1])
x=i;
}

that searches the rows sequantially you could implement the binary search methid as the upper bound method because accirding to the description of the task each first element of the row is greater than the last element of the previous row. And then for the selected row you could use standard C function bsearch.

Here is a C++ program shown as a pseudo-code that demonstrates ideas how the function can be implemented based on the binary search method applied to rows of the matrix and then to the selected row.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;
bool searchMatrix( const std::vector&lt;std::vector&lt;int&gt;&gt; &amp;matrix, int target )
{
std::vector&lt;int&gt; value = { target };
auto row = std::upper_bound( std::begin( matrix ), std::end( matrix ), value,
[]( const auto &amp;row1, const auto &amp;row2 ) { return row1[0] &lt; row2[0]; } );
bool success = row != std::begin( matrix );
if (success)
{
std::advance( row, -1 );
size_t size = row-&gt;size();
success = !( ( *row )[size - 1] &lt; target ) &amp;&amp;
std::binary_search( std::begin( *row ), std::end( *row ), target );
}
return success;
}
int main()
{
std::vector&lt;std::vector&lt;int&gt;&gt; matrix =
{
{  1,  3,  5,  7 },
{ 10, 11, 16, 20 },
{ 23, 30, 34, 60 }
};
std::cout &lt;&lt; std::boolalpha;
std::cout &lt;&lt; &quot;searchMatrix( matrix, 0 )  is &quot; &lt;&lt; searchMatrix( matrix, 0 )  &lt;&lt; &#39;\n&#39;;
std::cout &lt;&lt; &quot;searchMatrix( matrix, 8 )  is &quot; &lt;&lt; searchMatrix( matrix, 8 )  &lt;&lt; &#39;\n&#39;;
std::cout &lt;&lt; &quot;searchMatrix( matrix, 3 )  is &quot; &lt;&lt; searchMatrix( matrix, 3 )  &lt;&lt; &#39;\n&#39;;
std::cout &lt;&lt; &quot;searchMatrix( matrix, 13 ) is &quot; &lt;&lt; searchMatrix( matrix, 13 ) &lt;&lt; &#39;\n&#39;;
std::cout &lt;&lt; &quot;searchMatrix( matrix, 60 ) is &quot; &lt;&lt; searchMatrix( matrix, 60 ) &lt;&lt; &#39;\n&#39;;
std::cout &lt;&lt; &quot;searchMatrix( matrix, 61 ) is &quot; &lt;&lt; searchMatrix( matrix, 61 ) &lt;&lt; &#39;\n&#39;;
}

The program output is

searchMatrix( matrix, 0 )  is false
searchMatrix( matrix, 8 )  is false
searchMatrix( matrix, 3 )  is true
searchMatrix( matrix, 13 ) is false
searchMatrix( matrix, 60 ) is true
searchMatrix( matrix, 61 ) is false

That is in your function implementation instead of the first for loop

for(int i=0;i&lt;matrixSize;i++){
if(target&lt;=matrix[i][*matrixColSize-1])
x=i;
}

you should use the binary search method (written by you yourself) to find the target row. In the shown C++ program above it is the standard algorithm std::upper_bound.

And then instead of the second for loop you should use standard C function bsearch because as it is written in the assignment

> You must write a solution in O(log(m * n)) time complexity.

It is the main requirement of the assignment. That is you shall not use the sequential search within the function.

Update. I would write the function in C the following way

bool searchMatrix( int **matrix, int matrixSize, int *matrixColSize, int target ) 
{
bool success = matrixSize &gt; 0 &amp;&amp; *matrixColSize &gt; 0;
if (success)
{
int target_row = 0, last_row = matrixSize;
while (target_row &lt; last_row)
{
int middle_row = target_row + ( last_row - target_row ) / 2;
if (target &lt; matrix[middle_row][0])
{
last_row = middle_row;
}
else
{
target_row = middle_row + 1;
}
}
success = target_row != 0;
if ( success )
{
--target_row;
success = !( matrix[target_row][*matrixColSize - 1] &lt; target ) &amp;&amp;
bsearch( &amp;target, matrix[target_row], *matrixColSize, sizeof( int ), cmp ) != NULL;
}
}
return success;
}

I used the declaration presented in the site though as I already pointed out there is neither sense to pass the number of columns through a pointer to it.

The function cmp can look the following way

int cmp( const void *p1, const void *p2 )
{
int x = *( const int * )p1;
int y = *( const int * )p2;
return ( y &lt; x ) - ( x &lt; y );
}

huangapple
  • 本文由 发表于 2023年7月28日 04:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定