C++ 和 Python 实现旋转数组的最小数字(2)

# Define ParamError Exception
class ParamError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)


# Find the minimum in rotated array   
def min_in_rotated_array(data, length):
    if data is None or length <= 0:
        raise ParamError("Error: input parameters exception!")
    # Index initialization
    sta, mid, end = 0, 0, length-1
    # Ensure this requisite before binary search
    while data[sta] >= data[end]:
        if end - sta == 1:
            mid = end
            break
        # Get the middle index
        mid = (sta + end) / 2
        # Find the minimum in order
        if (data[sta] == data[mid]) and (data[mid] == data[end]):
            minimum = data[sta]
            for i in range(sta+1, end+1):
                if minimum > data[i]:
                    minimum = data[i]
            return minimum

if data[sta] <= data[mid]:
            sta = mid
        elif data[end] >= data[mid]:
            end = mid

return data[mid]


def unitest():
    arr1 = [3, 4, 5, 1, 2]
    arr2 = [1, 0, 1, 1, 1]
    arr3 = [1, 1, 1, 0, 1]
    arr4 = [1, 2, 3, 4, 5]

print("The minimum of the rotated array [3, 4, 5, 1, 2] is %d." % min_in_rotated_array(arr1, 5));
    print("The minimum of the rotated array [1, 0, 1, 1, 1] is %d." % min_in_rotated_array(arr2, 5));
    print("The minimum of the rotated array [1, 1, 1, 0, 1] is %d." % min_in_rotated_array(arr3, 5));
    print("The minimum of the rotated array [1, 2, 3, 4, 5] is %d." % min_in_rotated_array(arr4, 5));

try:
        min_in_rotated_array(arr1, -2)
    except Exception, e:
        print "\nFunction call: min_in_rotated_array(arr1, -2)"
        print e


if __name__ == '__main__':
    unitest()

参考代码

1. targetver.h

#pragma once

// The following macros define the minimum required platform.  The minimum required platform
// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
// your application.  The macros work by enabling all features available on platform versions up to and
// including the version specified.

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600    // Change this to the appropriate value to target other versions of Windows.
#endif

2. stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

// TODO: reference additional headers your program requires here

3. stdafx.cpp

// stdafx.cpp : source file that includes just the standard includes
// MinNumberInRotatedArray.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

4. MinNumberInRotatedArray.cpp

// MinNumberInRotatedArray.cpp : Defines the entry point for the console application.
//

// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛

#include "stdafx.h"
#include<exception>

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/cf3cc0d7b3d794ad91f73d2970612ffd.html