• 剑指offer笔记-数组中的重复数字


    面试题3:数组中重复的数字

    1.在使用Dev C++运行C++代码时,提示了错误error: 'nullptr' was not declared in this scope

    这是因为nullptr是C++11引入的关键字,这个编译器没有支持C++11。

    解决办法有两个:

    (1)设置编译器或升级编译器到gcc4.8.1,完全支持C++11。

    (2)改代码,不使用C++11特性,nullptr是C++11引入,用来表示空指针,所以只要将nullptr替换为0或NULL就搞定了。

    2.sizeof(numbers) / sizeof(int)

    sizeof为求字节数的函数,优先级高于/。

    3.bool duplicate(int numbers[], int length, int* duplication)

    int duplication;

    duplicate(numbers, lengthNumbers, &duplication)

    完整代码:

    #include <cstdio>
    
    // 参数:
    //        numbers:     一个整数数组
    //        length:      数组的长度
    //        duplication: (输出) 数组中的一个重复的数字
    // 返回值:             
    //        true  - 输入有效,并且数组中存在重复的数字
    //        false - 输入无效,或者数组中没有重复的数字
    bool duplicate(int numbers[], int length, int* duplication)
    {
        if(numbers == NULL || 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;
    }
    
    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.
    ");
                else
                    printf("FAILED.
    ");
            }
            else
                printf("Passed.
    ");
        }
        else
            printf("FAILED.
    ");
    }
    
    // 重复的数字是数组中最小的数字
    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 = NULL;
        int duplications[] = { -1 }; // not in use in the test function
        test("Test6", numbers, 0, duplications, sizeof(duplications) / sizeof(int), false);
    }
    
    int main(void)
    {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
    }
    https://github.com/zhedahht/CodingInterviewChinese2/blob/master/03_01_DuplicationInArray/FindDuplication.cpp
  • 相关阅读:
    Use Gravatar in ASP.NET
    Silverlight ToolkitPivotViewer
    The Future of Silverlight December 2, 2010 at 9:00
    WPF杂记…
    Windows Phone 7开发者站点
    安装 Internet Explorer 9 Beta 的先决条件
    Internet Explorer 9 Beta(多图)
    Expression Blend4 中文
    Silverlight and WPF Virtual books
    Server2008 安装 Zune
  • 原文地址:https://www.cnblogs.com/ZhangWj-/p/12937795.html
Copyright © 2020-2023  润新知