有一个练习要求一个特定数组中的最大子数组,并且子数组不能有重复的元素。
I do not have to worry about complexity and efficiency right now
数组是[8,2,2,3,4,1,6,5,1,7]
据我了解,没有重复元素的最大子数组是: [2,3,4,1,6,5,7]
但是他们说我的函数应该输出 [2,3,4,1,6,5]
为什么?
这是我的代码:
int existe(int chk, int v[], int tam){
int i;
for(i=0; i<tam; i++){
if(v[i]==chk){
return 1;
}
}
return 0;
}
int removeb(int v[], int n){
int i, max, j;
max = 0;
for(i=0; i<n; i++){
if(v[i]>max){
max = v[i];
}
}
for(i=0; i<n; i++){
if(v[i]==max){
j=i;
while(j<n){
v[j]=v[j+1];
j++;
}
return j-1;
}
}
return n;
}
int maxuniqueseq(int v[], int N){
int i, j, newsize;
int tmp[N];
//newsize = removeb(v, N);
printf("%d\n", N);
for(i=0; i<N; i++){
tmp[i] = v[i];
}
for(i=0, j=0; i<N; i++){
if(existe(tmp[i],v, i)){
;
}else{
v[j]=tmp[i];
j++;
}
}
newsize = removeb(v, j);
return newsize;
}
Output with the above array:
[2,3,4,1,6,5,7]
With another example:
[8,2,2,3,4,12,6,5,1,7]
outputs: [8,2,3,4,6,5,1,7]
最佳答案
我认为问题在于您对子数组的解释。
这就是子数组
A subarray is commonly defined as a part or section of an array.
因此,您选择的元素必须连续。
举个例子,
suppose [10, 20, 50, 15, 10, 25, 45] is the array,
a sub-array for this would be [10, 20, 50]
where as [10, 50, 25] can not be called as a sub-array.
And the biggest sub-array without repeated elements would be [ 20, 50, 15, 10, 25, 45]
所以改变你的代码再试一次!
关于c - 从一个数组中获取子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31103701/