简体   繁体   English

NSArray方法算法逻辑

[英]NSArray Method Algorithm logic

I am very new to the Objective-C language and I was asked a question in the interview that I was not able to answer. 我对Objective-C语言非常陌生,在采访中有人问我一个无法回答的问题。

I tried a lot of approaches and logic to no success. 我尝试了很多方法和逻辑都没有成功。 Here looking for an answer: 在这里寻找答案:

The question was: 问题是:

 - (NSArray *)reorderTheArraysAndMergeThemInDescendingOrder:(NSArray *)firstArray and:(NSArray *)secondArray

Both the provided arrays in the argument are already sorted in ascending order. 参数中提供的两个数组都已经按照升序排序。

My task was to provide an array as the return variable which will have all the unique elements of both firstArray and the secondArray and will have them in theh descending order. 我的任务是提供一个数组作为返回变量,该数组将具有firstArray和secondArray的所有唯一元素,并将它们按降序排列。 I was not allowed to use the inbuilt sorting functionality of Objective-C (that was the way I have been doing it ever since I am in this field). 不允许我使用Objective-C的内置排序功能(自从我进入该领域以来,我一直在这样做)。 My variables may only be primitives (the result will be an integer array of course.). 我的变量只能是基元(结果将是整数数组)。

I am new to the whole programming scene altogether and a good answer here will be MUCH appreciated. 我完全是整个编程领域的新手,这里的一个很好的答案将非常感激。

Shukaku 修学

Since the provided arrays are sorted in ascending order, this question is pretty-much related to the classic algorithm of Mergesort (have a look at it if you've never heard of it, this is a classic). 由于提供的数组是按升序排序的,所以这个问题与Mergesort的经典算法非常相关(如果您从未听说过,请查看一下,这是经典的)。

Since you're array contain only primitive data types, they can be compared using < 由于您的数组仅包含原始数据类型,因此可以使用<

The code that interest you would look like 您可能会感兴趣的代码

- (NSArray *)reorderAndMergeReverse:(NSArray *)array1 and:(NSArray *)array2{

    NSMutableArray *result = [NSMUtableArray array];

     int i = array1.count-1;
     int j = array2.count-1;

     while (result.count < array1.count+array2.count){

        if ([array1 objectAtIndex:i] > [array2 objectAtIndex:j]){
             [result addObject:[array1 objectAtIndex:i]];
             i--:
        } else {
            [result addObject:[array2 objectAtIndex:j]];
             j--:
        }

     }
     return result;
}

I am not sure as to whether you want to remove duplicates, your question is not that clear, would you mind telling me if so so that i can improve my code ? 我不确定您是否要删除重复项,您的问题不清楚,请您告诉我是否这样,以便我改进我的代码?
Edit : Did it anyway : since you're array is sorted, duplicates are just next to each other, so it might be easy not to add them in the first place; 编辑:还是这样做了:由于您对数组进行了排序,因此重复项彼此相邻,因此不首先添加它们就很容易了;

- (NSArray *)reorderAndMergeReverse:(NSArray *)array1 and:(NSArray *)array2{

    NSMutableArray *result = [NSMUtableArray array];

     int i = array1.count-1;
     int j = array2.count-1;
     int k = 0;

     while (i>=0 || j>=0){

        if ([array1 objectAtIndex:i] > [array2 objectAtIndex:j]){
             if ([array1 objectAtIndex:i] != [result objectAtIndex:k]){
                 [result addObject:[array1 objectAtIndex:i]];
                 k++;
             }
             i--:
        } else {
            if (array2 objectAtIndex:j] != [result objectAtIndex:k]){
                   [result addObject:[array2 objectAtIndex:j]];
                   k++;
             }
             j--:
        }

     }
     return result;
}

Edit : There are some errors in that code, because i could become negative and afterward be used in objectAtIndex: , this is just meant to demonstrate the idea, not to give an out-of-the-box code solution 编辑:该代码中有一些错误,因为i可能会变成负数,然后在objectAtIndex: ,这仅是为了演示该思想,而不是提供即用的代码解决方案

There are many ways to approach this. 有很多方法可以解决此问题。 You could reverse the arrays, then insert items from the second into the first, or you could append the second to the first then resort them. 您可以反转数组,然后从第二个数组插入第一个数组,也可以将第二个数组附加到第一个数组,然后重新排序。

I will post code later demonstrating the simplest way I can find. 稍后我将发布代码,展示我可以找到的最简单方法。 How fast and efficient does it have to be? 它必须有多快和高效? Can it be anything as long as it gets the job done? 只要完成工作,可以是任何东西吗?

Edit: It appears that Olotiarhas beat me to it. 编辑:看来奥洛蒂亚哈斯击败了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM