数组C ++中最长的连续序列

时间:2020-02-06 02:55:03

标签: c++ arrays

我必须创建一个函数,该函数将找到数组中最长的连续整数序列。

数组是这样的:

function url_get_contents ($Url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); return $output; }

注意:序列中重复的数字将被跳过。

这是我的代码:

$res = @url_get_contents($url);

我的代码不断为此数组返回2,但显然应该为5。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

#include <iostream>

void Log(int idx, int currentSeq, int maxSeq) {
  printf("current[%d] = %d, max[%d] = %d\n", idx, currentSeq, idx, maxSeq);
}

int LongestSequence(int* arr, int size) {
  int currentSeq = 1;
  int maxSeq = 1;  // max should be initialized as well
  Log(0, currentSeq, maxSeq);

  for (int i = 1; i < size; i++) {
    if (arr[i] == arr[i - 1]) {
    } else if (arr[i - 1] == (arr[i] - 1)) {
      currentSeq++;
    } else {
      currentSeq = 1;
    }
    // maxSeq should be updated in time, otherwise it would be tossed away
    maxSeq = std::max(maxSeq, currentSeq);
    Log(i, currentSeq, maxSeq);
  }

  return maxSeq;
}

int main() {
  int arr[] = {1, 2, 3, 4, 4, 4, 5, 7, 9, 10};
  std::cout << LongestSequence(arr, sizeof(arr) / sizeof(arr[0])) << std::endl;
}

输出:

current[0] = 1, max[0] = 1
current[1] = 2, max[1] = 2
current[2] = 3, max[2] = 3
current[3] = 4, max[3] = 4
current[4] = 4, max[4] = 4
current[5] = 4, max[5] = 4
current[6] = 5, max[6] = 5
current[7] = 1, max[7] = 5
current[8] = 1, max[8] = 5
current[9] = 2, max[9] = 5
5

答案 1 :(得分:1)

通过使用提供的示例数组在调试器中运行程序,我确定变量currentSeq确实达到了值5,并且该值5正确写入了maxSeq。但是,在程序的后面,maxSeq被值1和2覆盖。

在覆盖maxSeq之前,必须确定它是否已经包含比currentSeq高的值。在到达阵列末尾的情况下,您已经在一种情况下执行了此操作。但是在其他情况下,您不会这样做。

为使这种比较有效,您还必须初始化maxSeq,而不仅仅是currentSeq。否则,maxSeq可能包含非常大的数字,并且始终大于currentSeq

答案 2 :(得分:1)

您的数组中有3个序列:

  • 1、2、3、4、4、4、5,其中有5个连续数字。
  • 5、7不是连续的,并且将返回1。
  • 7,9也会返回一个。
  • 9,10,其中有2个连续的,将返回两个。

在循环中,您正在这样做:

for (int i = 1; i < size; i++)
{
    if (arr[i] == arr[i-1]);//skip repeated numbers

    else if (arr[i-1] == (arr[i] - 1))//check if index is 1 greater than last
    {
        currentSeq++;
    }
    else //if not reset and hold last sequence value as max
    {
        maxSeq = currentSeq; // <-- You're resetting your maxSequence even if
                             // currentSeq is smaller.
        currentSeq = 1;
    }
}

如下更改循环:

for (int i = 1; i < size; i++)
{
    if (arr[i] == arr[i-1])
        continue; //It is a good practice to skip the rest of the loop's 
                  //checks if you already know you're not gonna need them.
                  //The continue keyword skips only the current iteration of 
                  //the loop and improves intent readability.
    else if (arr[i-1] == (arr[i] - 1))//check if index is 1 greater than last
    {
        currentSeq++;
    }
    else //if not reset and hold last sequence value as max
    {
        currentSeq = 1; //This line stays.
                        //You only want to reset it under these specific conditions.
    }

    if (currentSeq > maxSeq) // Now you'll get the last sequence as well.
        maxSeq = currentSeq;
}

您可以在循环外删除该支票,然后直接返回。如果最后一个currentSeq大于maxSeq,则会被正确注册。

此外,当我进行此更改时,我遇到了编译错误,因为循环内的新if (currentSeq > maxSeq)试图在设置之前先读取maxSeq。因此,将maxSeq的声明更改为int maxSeq = 0

通过这些更改,我得到了期望值5。