[英]Find the length of the longest increasing subsequence
I've written some code in C that, given a sequence, should find the length of the longest increasing subsequence. 我已经用C语言编写了一些代码,给出了一个序列,应该可以找到最长的递增子序列的长度。 However it always returns the length 4 when this is incorrect.
但是,如果不正确,它将始终返回长度4。
Here is my code so far: 到目前为止,这是我的代码:
int LIS(int* seq, int* temp_seq, int seq_size)
{int i, j;
for(i=0; i<seq_size; i++);
{
temp_seq[i]=0;
}
for(i=1; i<seq_size; i++);
{
for(j=1; j<seq_size; j++)
{
if (seq[i]<temp_seq[j])
seq[i]=temp_seq[j];
else if (seq[i]>temp_seq[j])
seq[i]=temp_seq[j+1];
}
}
return(sizeof temp_seq);
}
What am I doing wrong? 我究竟做错了什么?
Also I should note that the sequence lists all integers from 1 to n with no repeated numbers. 我还要注意,该序列列出了从1到n的所有整数,没有重复的数字。
Returning wrong value. 返回错误的值。
return(sizeof temp_seq);
is a constant, the size of the pointer temp_seq
. 是一个常数,指针
temp_seq
的大小。 @eurythmia @eurythmia
The ;
的
;
after for(i=0; i<seq_size; i++);
在
for(i=0; i<seq_size; i++);
is certainly not needed as that completes the loop. 当然不需要,因为这样可以完成循环。 Same for
for(i=1; i<seq_size; i++);
对于
for(i=1; i<seq_size; i++);
. 。
The value of seq[0]
is never tested nor saved in temp_seq
. 从未测试
seq[0]
的值,也不将其保存在temp_seq
。
Possible access passed end of array with seq[i]=temp_seq[j+1];
可能的访问通过数组
seq[i]=temp_seq[j+1];
Code appears to want to clear temp_seq[]
and then set all seq[]
with temp_seq[]
. 代码似乎想要清除
temp_seq[]
,然后使用temp_seq[]
设置所有seq[]
temp_seq[]
。
tmp_seq
is declared in the function defintion as a pointer to type int
. tmp_seq
在函数定义中声明为指向int
类型的指针。 Your function is therefore always returning the size of an int *
(which happens to be 4 bytes in this case). 因此,您的函数总是返回
int *
的大小(在这种情况下恰好是4个字节)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.