C 递归反转长度为2^n的整数数组,并在不修改原始数组的情况下返回新数组
我在一次采访中遇到了以下问题 完成此函数以返回反向数组,而不修改函数签名或原始数组。注意,这里根本不应该使用静态数据类型 假设arrayLength是2的幂。i、 e 2^n.->我想这就是诀窍C 递归反转长度为2^n的整数数组,并在不修改原始数组的情况下返回新数组,c,arrays,pointers,recursion,C,Arrays,Pointers,Recursion,我在一次采访中遇到了以下问题 完成此函数以返回反向数组,而不修改函数签名或原始数组。注意,这里根本不应该使用静态数据类型 假设arrayLength是2的幂。i、 e 2^n.->我想这就是诀窍 int* reverse(int *array, int arrayLength){ } 请帮忙。 注意,我真的想不出解决这个问题的办法。采访者暗示要用2^n作为puspose,但我真的想不出解决方案。好吧,这里有一个狡猾的方法,它不在乎数组的长度。注意:我假设您不能引入新函数,它必须在现有函数中完
int* reverse(int *array, int arrayLength){
}
请帮忙。
注意,我真的想不出解决这个问题的办法。采访者暗示要用2^n作为puspose,但我真的想不出解决方案。好吧,这里有一个狡猾的方法,它不在乎数组的长度。注意:我假设您不能引入新函数,它必须在现有函数中完成 如果长度为正数,则分配内存并进行复制,然后再次使用负长度调用reverse并复制,如果使用负长度调用函数,则反转第一个和最后一个在位,然后通过移动到数组中的下一个来递归调用,并收缩长度,直到没有任何东西可以反转,然后递归函数展开
int* reverse(int *array, int arrayLength){
int* result;
if(arrayLength > 0)
{
result =(int*) malloc((sizeof(int)*arrayLength));
memcpy(result, array, sizeof(int)*arrayLength);
reverse(result, -arrayLength);
return result;
}
else if(arrayLength < -1)
{
int end = (-arrayLength)-1;
int temp = array[end];
array[end] = array[0];
array[0] = temp;
return reverse(array+1, arrayLength+2);
}
return array;
}
int*反向(int*数组,int数组长度){
int*结果;
如果(阵列长度>0)
{
结果=(int*)malloc((sizeof(int)*arraylelength));
memcpy(结果、数组、大小f(int)*数组长度);
反向(结果,-排列长度);
返回结果;
}
否则如果(排列长度<-1)
{
int end=(-arraylelength)-1;
int temp=数组[结束];
数组[end]=数组[0];
数组[0]=temp;
返回反向(数组+1,数组长度+2);
}
返回数组;
}
考虑到阵列长度始终是2的幂。我们将把函数应用于数组的两个部分,然后以相反的方式将它们合并。
最后,如果数组只有一个元素,我们只需返回具有相同元素的其他数组
int* reverse(int *array, int arrayLength){
int * newArray = NULL;
if(arrayLength == 1){
newArray = (int *)malloc(sizeof(int));
*newArray = array[0];
} else if(arrayLength == 2){
newArray = (int *)malloc(2 * sizeof(int));
newArray[0] = array[1];
newArray[1] = array[0];
} else {
// apply to first half
int * first = reverse(array, arrayLength / 2);
// apply to second half
int * second = reverse(array + arrayLength / 2, arrayLength / 2);
// allocate space
newArray = (int *) malloc(arrayLength * sizeof(int));
// copy parts in reverse way
memcpy(newArray, second, arrayLength / 2 * sizeof(int));
memcpy(newArray + arrayLength / 2, first, arrayLength / 2 * sizeof(int));
// free allocated space for parts
free(first);
free(second);
}
return newArray;
}
我要试一试
知道数组的长度为2^n意味着可以安全地将其减半。我们对每一半递归调用函数,直到长度为2。在这一点上,我们交换两个整数。想想{2,1,4,3,6,5,8,7}
。当我们从那里回来时,每一半被合并到它来自的相反位置({4,3,2,1,8,7,6,5}
)。冲洗并重复
#include <stdio.h>
#include <stdlib.h>
int * reverse( int* arr, int length )
{
if ( length == 1 )
{
int *result = malloc( sizeof( arr[0] ) );
result[0] = arr[0];
return result;
}
int * result = 0;
if ( length == 2 )
{
result = malloc( sizeof( arr[0] ) * 2 );
result[0] = arr[1];
result[1] = arr[0];
}
else
{
int half_length = length / 2;
// named correctly
int * right = reverse( arr, half_length );
int * left = reverse( arr + half_length, half_length );
result = malloc( sizeof( arr[0] ) * length );
for ( int i = 0; i < half_length; ++i )
{
result[i] = left[i];
result[ i + half_length ] = right[i];
}
free( right );
free( left );
}
return result;
}
int main( void )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int length = 8;
int *reversed = reverse( arr, length );
for ( int i = 0; i < length; ++i )
{
printf( "%d %d\n", arr[i], reversed[i] );
}
free( reversed );
return 0;
}
#包括
#包括
int*反向(int*arr,int-length)
{
如果(长度==1)
{
int*result=malloc(sizeof(arr[0]);
结果[0]=arr[0];
返回结果;
}
int*result=0;
如果(长度==2)
{
结果=malloc(sizeof(arr[0])*2);
结果[0]=arr[1];
结果[1]=arr[0];
}
其他的
{
int half_length=长度/2;
//命名正确
int*right=反向(arr,半长);
int*left=反向(arr+半长,半长);
结果=malloc(sizeof(arr[0])*长度);
对于(int i=0;i
int*反向(int*数组,int数组长度){
if(arrayLength==0)返回数组;
int*ret=(int*)malloc(arraylelength*sizeof(int));
对于(inti=0;i
int* reverse(int *array, int arrayLength){
if (arrayLength==1) {
int* out=(int*)malloc(sizeof(int));
out[0] = array[0];
return out;
}
int* left = reverse(array+arrayLength/2, arrayLength-arrayLength/2);
int* right = reverse(array,arrayLength/2);
int* out = (int*)realloc(left, sizeof(int)*arrayLength);
memcpy(out+arrayLength/2, right, sizeof(int)*(arrayLength/2));
free(right);
return out;
}
在这里(和工程):
它可以在没有临时存储的情况下完成,但您必须进行一点迭代
int *reverse(int *a, int al)
{
if (al > 1) {
int i, a1 = al >> 1;
for (i = 0; i < a1; i++) {
int temp = a[i];
a[i] = a[i + a1];
a[i + a1] = temp;
} /* for */
reverse(a, a1);
reverse(a+a1, a1);
} /* for */
return a;
} /* reverse */
同意OP的建议,提示是“2^n”。和许多递归函数一样:分治
这个例程首先处理错误的参数和简单的长度。接下来,将长度一分为二,每一半反转。通过连接反转的左、右子数组形成结果。首先,右,然后左
通常的清理工作如下
#include <string.h>
#include <stdlib.h>
int* reverse(int *array, int arrayLength) {
// Check parameters
if (array == NULL || arrayLength < 0) {
; // TBD HandleBadParameters();
}
// Allocate space for result, not much to do if length <= 1
int *y = malloc(arrayLength * sizeof *y);
if (y == NULL) {
; // TBD HandleOOM();
}
if (arrayLength <= 1) {
memcpy(y, array, arrayLength * sizeof *y);
return y;
}
// Find reverse of the two halves
int halflength = arrayLength / 2;
int *left = reverse(array, halflength);
int *right = reverse(&array[halflength], halflength);
// Append them to the result - in reverse order
memcpy(y, right, halflength * sizeof *y);
memcpy(&y[halflength], left, halflength * sizeof *y);
// Clean-up and return
free(right);
free(left);
return y;
}
#包括
#包括
int*反向(int*数组,int数组长度){
//检查参数
if(array==NULL | | arrayLength<0){
;//待定把手参数();
}
//为结果分配空间,如果长度对于所有包含2个以上元素的整数数组,则无需做太多工作。
基本思想是交换两端的元素,直到剩余的元素数为1
int*反向数组(int*数组,int数组长度)
{
if(arrayLength)和…你又做了什么?这里有一些难读的代码,它能用吗?int a=0,z=arrayLength-1;而(athats not recursive@GradyPlayerah dammit什么是递归帮助…除了在一个足够大的数组上崩溃堆栈之外什么都没有…@webNeat另一方面,整个递归函数都是在浪费时间。我想知道这个问题是否更多地是关于遵循诸如驼峰大小写变量名之类的规则。可能是bEST要转到<代码>长度=1 < /代码>您也忘了考虑<代码>边缘长度=1 < <代码>的边缘情况(参见上面的评论和@ WebLead)。我把它作为一个边缘案例,因为它将使递归调用的数量减少一半。我喜欢所有对这些答案的无注释否决票。memcpy
不是必需的,for循环
应该一直到iI在上一个解决方案中,建议类似于b=arraylelength-1
…而不修改函数签名或者原始数组,没错。我考虑过了,但写的时候忘了写…谢谢你的观点!如果不修改原始数组,在复制后要将不同的数组连接起来并删除,还有很多工作要做。我不想做这么低效的代码。
int *reverse(int *a, int al)
{
if (al > 1) {
int i, a1 = al >> 1;
for (i = 0; i < a1; i++) {
int temp = a[i];
a[i] = a[i + a1];
a[i + a1] = temp;
} /* for */
reverse(a, a1);
reverse(a+a1, a1);
} /* for */
return a;
} /* reverse */
int *reverse(int *array, int arrayLength)
{
int a, b;
for (a = 0, b = arrayLength-1; a < b; a++, b--) {
int temp = array[a];
array[a] = array[b];
array[b] = temp;
} /* for */
return array;
} /* reverse */
int *reverse(int *array, int arrayLength)
{
int *a1, *a2;
int *res;
if (arrayLength > 1) {
int l = arrayLength >> 1;
a1 = reverse(array, l);
a2 = reverse(array + l, l);
res = calloc(arrayLength, sizeof(int));
memcpy(res, a2, l*sizeof(int));
memcpy(res+l, a1, l*sizeof(int));
free(a1);
free(a2);
} else {
/* we return always memory alloc'd with malloc() so we have to do this. */
res = malloc(sizeof(int));
*res = array[0];
} /* if */
return res;
} /* reverse */
#include <string.h>
#include <stdlib.h>
int* reverse(int *array, int arrayLength) {
// Check parameters
if (array == NULL || arrayLength < 0) {
; // TBD HandleBadParameters();
}
// Allocate space for result, not much to do if length <= 1
int *y = malloc(arrayLength * sizeof *y);
if (y == NULL) {
; // TBD HandleOOM();
}
if (arrayLength <= 1) {
memcpy(y, array, arrayLength * sizeof *y);
return y;
}
// Find reverse of the two halves
int halflength = arrayLength / 2;
int *left = reverse(array, halflength);
int *right = reverse(&array[halflength], halflength);
// Append them to the result - in reverse order
memcpy(y, right, halflength * sizeof *y);
memcpy(&y[halflength], left, halflength * sizeof *y);
// Clean-up and return
free(right);
free(left);
return y;
}
if(arrayLength <2)
{
return NULL;
}
else
{
int *array1 = NULL;
int *array2 = NULL;
array1 = malloc(arrayLength*sizeof(int));
memcpy(array1,array,arrayLength*sizeof(int));
/*swap the start and end*/
swap(array1,(array1+arrayLength-1));
/* swap the next pair */
array2 = reverse_array(array1+1,arrayLength-2);
memcpy(array1+1,array2,(arrayLength-2)*sizeof(int));
if(array2!= NULL)
{
free(array2);
}
return array1;
}