剑指offer笔记面试题3----数组中重复的数字

题目一:找出数组中重复的数字。在一个长度为n的数组里的所有数字都在0~n-1的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为7的数组{2, 3, 1, 0, 2, 5, 3},那么对应的输出是重复的数字2或者3。

测试用例:

  • 长度为n的数组里包含一个或多个重复的数字。
  • 数组中不包含重复的数字。
  • 无效输入测试用例(输入空指针;长度为n的数组中包含0~n-1之外的数字)

测试代码:

    void test(char* testName, int numbers[], int lengthNumbers, int expected[], int expectedExpected, bool validArgument)
    {
        printf("%s begins: ", testName);
        int duplication;
        bool validInput = duplicate(numbers, lengthNumbers, &duplication);
    
        if(validArgument == validInput)
        {
            if(validArgument)
            {
                if(contains(expected, expectedExpected, duplication))
                    printf("Passed.\n");
                else
                    printf("FAILED.\n");
            }
            else
                printf("Passed.\n");
        }
        else
            printf("FAILED.\n");
    }
    
    // 重复的数字是数组中最小的数字
    void test1()
    {
        int numbers[] = { 2, 1, 3, 1, 4 };
        int duplications[] = { 1 };
        test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
    }
    
    // 重复的数字是数组中最大的数字
    void test2()
    {
        int numbers[] = { 2, 4, 3, 1, 4 };
        int duplications[] = { 4 };
        test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
    }
    
    // 数组中存在多个重复的数字
    void test3()
    {
        int numbers[] = { 2, 4, 2, 1, 4 };
        int duplications[] = { 2, 4 };
        test("Test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
    }
    
    // 没有重复的数字
    void test4()
    {
        int numbers[] = { 2, 1, 3, 0, 4 };
        int duplications[] = { -1 }; // not in use in the test function
        test("Test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
    }
    
    // 没有重复的数字
    void test5()
    {
        int numbers[] = { 2, 1, 3, 5, 4 };
        int duplications[] = { -1 }; // not in use in the test function
        test("Test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
    }
    
    // 无效的输入
    void test6()
    {
        int* numbers = nullptr;
        int duplications[] = { -1 }; // not in use in the test function
        test("Test6", numbers, 0, duplications, sizeof(duplications) / sizeof(int), false);
    }

本题考点:

  • 考查应聘者对以为数组的理解及编程能力。一维数组在内存中占据连续的空间,因此我们可以根据下标定位对应的元素。
  • 考查应聘者分析问题的能力。当应聘者发现问题比较复杂时,能不能通过具体的例子找出其中的规律,是能否解决这个问题的关键所在。

解法:

  • 先把输入的数组排序,再从头扫描排序后的数组就可以了。排序一个长度为n的数组需要O(nlogn)的时间。
  • 利用哈希表,时间复杂度O(n),空间复杂度O(n)。
  • 最优解可以做到时间复杂度O(n),空间复杂度O(1)。
    我们注意到数组中的数字都在0~n-1的范围内。如果这个数组中没有重复的数字,那么当数组排序之后数字i将出现在下标为i的位置。由于数组中有重复的数字,有些位置可能存在多个数字,同时有些位置可能没有数字。
    现在让我们重排这个数组。从头到尾以此扫描这个数组中的每个数字。当扫描到下标为i的数字时,首先比较这个数字(用m表示)是不是等于i。如果是,则接着扫描下一个数字;如果不是,则再拿它和第m个数字进行比较。如果它和第m个数字相等,就找到了一个重复的数字(该数字在下标为i和m的位置都出现了);如果它和第m个数字不相等,就把第i个数字和第m个数字交换,把m放到属于它的位置。接下来再重复这个比较、交换的过程,知道我们发现一个重复的数字。
    example:
    {2, 3, 1, 0, 2, 5, 3} ->{1, 3, 2, 0, 2, 5, 3}->{3, 1, 2, 0, 2, 5, 3}->{0, 1, 2, 3, 2, 5, 3}

实现代码:

    #include <cstdio>
    
    // 参数:
    //        numbers:     一个整数数组
    //        length:      数组的长度
    //        duplication: (输出) 数组中的一个重复的数字
    // 返回值:             
    //        true  - 输入有效,并且数组中存在重复的数字
    //        false - 输入无效,或者数组中没有重复的数字
    bool duplicate(int numbers[], int length, int* duplication)
    {
        if(numbers == nullptr || length <= 0)
            return false;
    
        for(int i = 0; i < length; ++i)
        {
            if(numbers[i] < 0 || numbers[i] > length - 1)
                return false;
        }
    
        for(int i = 0; i < length; ++i)
        {
            while(numbers[i] != i)
            {
                if(numbers[i] == numbers[numbers[i]])
                {
                    *duplication = numbers[i];
                    return true;
                }
                // 交换numbers[i]和numbers[numbers[i]]             
                int temp = numbers[i];
                numbers[i] = numbers[temp];
                numbers[temp] = temp;
            }
        }
        return false;
    }
    bool contains(int array[], int length, int number)
    {
        for(int i = 0; i < length; ++i)
        {
            if(array[i] == number)
                return true;
        }
        return false;
    }
    int main()
    {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        return 0;
    }

题目二:不修改数组找出重复的数字。在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的数组。例如,如果输入长度为8的数组{2, 3, 5, 4, 3, 2, 6, 7},那么对应的输出是重复的数字2或者3。

测试用例:

  • 长度为n+1的数组里包含一个或多个重复的数字。
  • 数组中不包含重复的数字。
  • 无效输入测试用例(输入空指针;长度为n的数组中包含1~n之外的数字)。

测试代码:

void test(const char* testName, int* numbers, int length, int* duplications, int dupLength)
{
    int result = getDuplication(numbers, length);
    for(int i = 0; i < dupLength; ++i)
    {
        if(result == duplications[i])
        {
            std::cout << testName << " passed." << std::endl;
            return;
        }
    }
    std::cout << testName << " FAILED." << std::endl;
}

// 多个重复的数字
void test1()
{
    int numbers[] = { 2, 3, 5, 4, 3, 2, 6, 7 };
    int duplications[] = { 2, 3 };
    test("test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 一个重复的数字
void test2()
{
    int numbers[] = { 3, 2, 1, 4, 4, 5, 6, 7 };
    int duplications[] = { 4 };
    test("test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 重复的数字是数组中最小的数字
void test3()
{
    int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 1, 8 };
    int duplications[] = { 1 };
    test("test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 重复的数字是数组中最大的数字
void test4()
{
    int numbers[] = { 1, 7, 3, 4, 5, 6, 8, 2, 8 };
    int duplications[] = { 8 };
    test("test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 数组中只有两个数字
void test5()
{
    int numbers[] = { 1, 1 };
    int duplications[] = { 1 };
    test("test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 重复的数字位于数组当中
void test6()
{
    int numbers[] = { 3, 2, 1, 3, 4, 5, 6, 7 };
    int duplications[] = { 3 };
    test("test6", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 多个重复的数字
void test7()
{
    int numbers[] = { 1, 2, 2, 6, 4, 5, 6 };
    int duplications[] = { 2, 6 };
    test("test7", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 一个数字重复三次
void test8()
{
    int numbers[] = { 1, 2, 2, 6, 4, 5, 2 };
    int duplications[] = { 2 };
    test("test8", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 没有重复的数字
void test9()
{
    int numbers[] = { 1, 2, 6, 4, 5, 3 };
    int duplications[] = { -1 };
    test("test9", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}

// 无效的输入
void test10()
{
    int* numbers = nullptr;
    int duplications[] = { -1 };
    test("test10", numbers, 0, duplications, sizeof(duplications) / sizeof(int));
}

本题考点:

  • 考查应聘者对以为数组的理解及编程能力。以为数组在内存中占据连续的空间,因此我们可以根据下标定位对应的元素。
  • 考查应聘者对二分查找算法的理解,并能快速、正确地实现二分查找算法的代码。
  • 考查应聘者的沟通能力。应聘者只有具备良好的沟通能力,才能充分了解面试官的需求,从而有针对性地选择算法解决问题。

实现代码:

#include <iostream>

int countRange(const int* numbers, int length, int start, int end);

// 参数:
//        numbers:     一个整数数组
//        length:      数组的长度
// 返回值:             
//        正数  - 输入有效,并且数组中存在重复的数字,返回值为重复的数字
//        负数  - 输入无效,或者数组中没有重复的数字
int getDuplication(const int* numbers, int length)
{
    if(numbers == nullptr || length <= 0)
        return -1;
    int start = 1;
    int end = length - 1;
    while(end >= start)
    {
        int middle = ((end - start) >> 1) + start;
        int count = countRange(numbers, length, start, middle);
        if(end == start)
        {
            if(count > 1)
                return start;
            else
                break;
        }
        if(count > (middle - start + 1))
            end = middle;
        else
            start = middle + 1;
    }
    return -1;
}

int countRange(const int* numbers, int length, int start, int end)
{
    if(numbers == nullptr)
        return 0;
    int count = 0;
    for(int i = 0; i < length; i++)
        if(numbers[i] >= start && numbers[i] <= end)
            ++count;
    return count;
}
void main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
}

猜你喜欢

转载自www.cnblogs.com/tangliang39/p/11694438.html