Rotating the array a minimum numeral 11 ---- prove safety notes offer face questions

Topic: the beginning of an array of several elements moved to the end of the array, the array we call rotation. Enter a sorted array an incremental rotation of the output rotary smallest element of the array. For example, the array {3, 4, 5, 1, 2} {1, 2, 3, 4, 5} of a rotation, the minimum number is one.

Test Case:

  • Function Test (a rotation of the input array is sorted in ascending order of the array, the array is not duplicated or repeated digital numbers).
  • Boundary value test (the input array is an array sorted in ascending order, contains only one array of numbers).
  • Special test input (input nullptr pointer).

Test code:

void Test(int* numbers, int length, int expected)
{
    int result = 0;
    try
    {
        result = Min(numbers, length);

        for(int i = 0; i < length; ++i)
            printf("%d ", numbers[i]);
        if(result == expected)
            printf("\tpassed\n");
        else
            printf("\tfailed\n");
    }
    catch (...)
    {
        if(numbers == nullptr)
            printf("Test passed.\n");
        else
            printf("Test failed.\n");
    }
}

This question test sites:

  • Examine the candidate's understanding of the binary search. This problem transform the binary condition are looking for, not the sort of input array, but an array of sort rotation. This requires us to have a deep understanding of the process of binary search.
  • Communication and learning ability test candidates. Interviewer asks this question, a new concept: the rotation of the array. We have to learn in a very short period of time, to understand this new concept. During the interview, the interviewer asked if the new concept, and then we can take the initiative to communicate with the interviewer, ask a few questions, the concept clear.
  • Examine the candidates comprehensive thinking. Sort the array itself is a special case of an array rotation. In addition, we have to take into account the array has the same number of exceptions. If you can not handle these special cases, it is difficult to write the interviewer satisfied with the perfect code.

Implementation code:

#include <cstdio>
#include <stdexcept>

int MinInOrder(int* numbers, int index1, int index2);

int Min(int* numbers, int length)
{
    if(numbers == nullptr || length <= 0)
        throw std::logic_error("Invalid parameters");
    int index1 = 0;
    int index2 = length - 1;
    int indexMid = index1;
    while(numbers[index1] >= numbers[index2])
    {
        // 如果index1和index2指向相邻的两个数,
        // 则index1指向第一个递增子数组的最后一个数字,
        // index2指向第二个子数组的第一个数字,也就是数组中的最小数字
        if(index2 - index1 == 1)
        {
            indexMid = index2;
            break;
        }
        // 如果下标为index1、index2和indexMid指向的三个数字相等,
        // 则只能顺序查找
        indexMid = (index1 + index2) / 2;
        if(numbers[index1] == numbers[index2] && numbers[indexMid] == numbers[index1])
            return MinInOrder(numbers, index1, index2);
        // 缩小查找范围
        if(numbers[indexMid] >= numbers[index1])
            index1 = indexMid;
        else if(numbers[indexMid] <= numbers[index2])
            index2 = indexMid;
    }
    return numbers[indexMid];
}

int MinInOrder(int* numbers, int index1, int index2)
{
    int result = numbers[index1];
    for(int i = index1 + 1; i <= index2; ++i)
    {
        if(result > numbers[i])
            result = numbers[i];
    }
    return result;
}
int main(int argc, char* argv[])
{
    // 典型输入,单调升序的数组的一个旋转
    int array1[] = { 3, 4, 5, 1, 2 };
    Test(array1, sizeof(array1) / sizeof(int), 1);

    // 有重复数字,并且重复的数字刚好的最小的数字
    int array2[] = { 3, 4, 5, 1, 1, 2 };
    Test(array2, sizeof(array2) / sizeof(int), 1);

    // 有重复数字,但重复的数字不是第一个数字和最后一个数字
    int array3[] = { 3, 4, 5, 1, 2, 2 };
    Test(array3, sizeof(array3) / sizeof(int), 1);

    // 有重复的数字,并且重复的数字刚好是第一个数字和最后一个数字
    int array4[] = { 1, 0, 1, 1, 1 };
    Test(array4, sizeof(array4) / sizeof(int), 0);

    // 单调升序数组,旋转0个元素,也就是单调升序数组本身
    int array5[] = { 1, 2, 3, 4, 5 };
    Test(array5, sizeof(array5) / sizeof(int), 1);

    // 数组中只有一个数字
    int array6[] = { 2 };
    Test(array6, sizeof(array6) / sizeof(int), 2);

    // 输入nullptr
    Test(nullptr, 0, 0);
    return 0;
}

Guess you like

Origin www.cnblogs.com/tangliang39/p/11693374.html