NSArray *a = @[@1,@3,@4,@5,@8,@9,@10,@32];
NSArray *b = @[@3,@4,@5];
NSLog(@"a:%@\b:%@",a,b);
int j=0,k=0;
int count = 0;
NSMutableArray *arr = [@[] mutableCopy];
while (j < a.count && k < b.count)
{
if (a[j] > b[k]) {
[arr addObject:b[k]];
k++;
}
else if (a[j] < b[k])
{
[arr addObject:a[j]];
j++;
}
else{
j++;
k++;
}
count ++;
}
int i = MAX(j, k);
if (a.count > b.count)
{
for (; i < a.count; i++)
{
[arr addObject:a[i]];
}
}
else
{
for (; i < b.count; i++)
{
[arr addObject:b[i]];
}
}
NSLog(@"%@,%d",arr,count);
利用归并去除两个数组重复部分
最新推荐文章于 2024-03-20 02:28:11 发布